C的話可以用nxc或者robotc,前者是免費(fèi)的,后者試用30天,前者用的人多,教程也多,建議用nxc
成都創(chuàng)新互聯(lián)是一家專業(yè)提供淇縣企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計(jì)、小程序制作等業(yè)務(wù)。10年已為淇縣眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。
給你個(gè)網(wǎng)
Server.java
import?java.io.DataInputStream;
import?java.io.DataOutputStream;
import?java.io.IOException;
import?java.net.ServerSocket;
import?java.net.Socket;
import?java.util.ArrayList;
import?java.util.List;
public?class?Server?{
private?ListServerThread?serverThreads;
private?ServerSocket?serverSocket;
public?Server()?throws?IOException?{
serverSocket?=?new?ServerSocket(8025);
serverThreads?=?new?ArrayListServerThread();
}
public?ListServerThread?getServerThreads()?{
return?serverThreads;
}
public?void?setServerThreads(ListServerThread?serverThreads)?{
this.serverThreads?=?serverThreads;
}
public?ServerSocket?getServerSocket()?{
return?serverSocket;
}
public?void?setServerSocket(ServerSocket?serverSocket)?{
this.serverSocket?=?serverSocket;
}
public?static?void?main(String[]?args)?{
Server?server?=?null;
try?{
server?=?new?Server();
}?catch?(IOException?e)?{
e.printStackTrace();
System.exit(-1);
}
try?{
Socket?socket;
Thread?t;
while?(true)?{
socket?=?server.getServerSocket().accept();
ServerThread?serverThread?=?new?ServerThread(server,?socket);
server.getServerThreads().add(serverThread);
t?=?new?Thread(serverThread);
t.start();
}
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
public?void?sendMessage(String?message,?Socket?socket)?{
for?(ServerThread?serverThread?:?serverThreads)?{
try?{
serverThread.getDos().writeUTF(message);
}?catch?(IOException?e)?{
//?e.printStackTrace();
System.out.println("推送消息給"?+?serverThread.getSocket().getRemoteSocketAddress()?+?"過程中發(fā)生了錯(cuò)誤.");
}
}
}
}
class?ServerThread?implements?Runnable?{
private?Server?server;
private?Socket?socket;
private?DataInputStream?dis;
private?DataOutputStream?dos;
public?ServerThread(Server?server,?Socket?socket)?throws?IOException?{
this.server?=?server;
this.socket?=?socket;
dis?=?new?DataInputStream(socket.getInputStream());
dos?=?new?DataOutputStream(socket.getOutputStream());
}
public?Server?getServer()?{
return?server;
}
public?void?setServer(Server?server)?{
this.server?=?server;
}
public?Socket?getSocket()?{
return?socket;
}
public?void?setSocket(Socket?socket)?{
this.socket?=?socket;
}
public?DataInputStream?getDis()?{
return?dis;
}
public?void?setDis(DataInputStream?dis)?{
this.dis?=?dis;
}
public?DataOutputStream?getDos()?{
return?dos;
}
public?void?setDos(DataOutputStream?dos)?{
this.dos?=?dos;
}
public?void?run()?{
String?message?=?"";
while?(!message.equals("logout"))?{
try?{
message?=?socket.getRemoteSocketAddress()?+?"?say:?"?+?dis.readUTF();
//?System.out.println(message);
server.sendMessage(message,?socket);
}?catch?(IOException?e)?{
//e.printStackTrace();
break;
}
}
server.getServerThreads().remove(this);
try?{
dis.close();
dos.close();
socket.close();
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
Client.java
import?java.io.DataInputStream;
import?java.io.DataOutputStream;
import?java.io.IOException;
import?java.net.Socket;
import?java.net.UnknownHostException;
import?java.util.Scanner;
public?class?Client?{
private?Socket?socket;
private?DataOutputStream?dos;
private?DataInputStream?dis;
public?Client()?throws?UnknownHostException,?IOException?{
socket?=?new?Socket("127.0.0.1",?8025);
dis?=?new?DataInputStream(socket.getInputStream());
dos?=?new?DataOutputStream(socket.getOutputStream());
new?Thread(new?ClientThread(this)).start();
}
public?Socket?getSocket()?{
return?socket;
}
public?void?setSocket(Socket?socket)?{
this.socket?=?socket;
}
public?DataOutputStream?getDos()?{
return?dos;
}
public?void?setDos(DataOutputStream?dos)?{
this.dos?=?dos;
}
public?DataInputStream?getDis()?{
return?dis;
}
public?void?setDis(DataInputStream?dis)?{
this.dis?=?dis;
}
public?void?close()?{
try?{
dis.close();
dos.close();
socket.close();
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
private?void?sendMessage(String?message)?{
try?{
dos.writeUTF(message);
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
public?static?void?main(String[]?args)?{
Client?client?=?null;
try?{
client?=?new?Client();
System.out.println(client.getSocket().getLocalPort());
}?catch?(IOException?e)?{
System.out.println("BUG");
e.printStackTrace();
System.exit(-1);
}
String?message?=?"";
Scanner?scanner?=?new?Scanner(System.in);
while?(!message.equals("logout"))?{
message?=?scanner.nextLine();
//?System.out.println(message);
client.sendMessage(message);
}
scanner.close();
client.close();
}
}
class?ClientThread?implements?Runnable?{
private?Client?client;
public?ClientThread(Client?client)?{
super();
this.client?=?client;
}
@Override
public?void?run()?{
try?{
while?(true)?{
System.out.println(client.getDis().readUTF());
}
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
ServerSocket ss = null;
Socket socket = ss.accept();
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());//向客戶端發(fā)送數(shù)據(jù)的輸出流
DataInputStream dis = new DataInputStream(socket.getInputStream());//收客戶端發(fā)來的數(shù)據(jù)的輸入流
只要接收到的dis.readUTF())值
dos.writeUTF("自動(dòng)回復(fù)的值");
JAVA 使用藍(lán)牙、WIFI,將命令傳輸給機(jī)器人。。。。。。。。機(jī)器人的控制,可復(fù)雜了。。。。。
使用Eclipse編寫自己的第一個(gè)Java代碼。
編寫如下:
1、首先打開自己安裝的Eclipse軟件。
2、然后選擇File-New-JavaProject。
3、然后找到工程打開,右擊src選擇-New-Class。
4、填入類名,選擇下面的publicstaticvoidmain(String[]args)。
5、然后寫入代碼publicclassDemo{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubSystem.out.println(HelloWorld)。
6、點(diǎn)擊上面的運(yùn)行按鈕,點(diǎn)擊確認(rèn),可以看到代碼運(yùn)行結(jié)果。
public class Test
{
public static void main(String args[])
{
System.out.println("機(jī)器人啟動(dòng)");
String s;
do{
Scanner sc=new Scanner(System.in);
s=sc.next();
if(s.equals("你好")){
System.out.println("你好!我是小P,請問你是誰?...... ");
}
else if(s.equals("小P回答")){
System.out.println("你好!我是小P你是誰?");
}
else if(s.equals("我是令狐沖")){
System.out.println("令狐沖 是你啊,好想你啊!");
}
else if(s.equals("再見")){
System.out.println("再見");
}
else{
System.out.println("對不起我不懂你在說什么!" );
}
}while(!s.equals("再見"));
}
}