十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
C的话可以用nxc或者robotc,前者是免费的,后者试用30天,前者用的人多,教程也多,建议用nxc
成都创新互联是一家专业提供淇县企业网站建设,专注与成都网站设计、网站建设、H5网站设计、小程序制作等业务。10年已为淇县众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。
给你个网
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() + "过程中发生了错误.");
}
}
}
}
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());//向客户端发送数据的输出流
DataInputStream dis = new DataInputStream(socket.getInputStream());//收客户端发来的数据的输入流
只要接收到的dis.readUTF())值
dos.writeUTF("自动回复的值");
JAVA 使用蓝牙、WIFI,将命令传输给机器人。。。。。。。。机器人的控制,可复杂了。。。。。
使用Eclipse编写自己的第一个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、点击上面的运行按钮,点击确认,可以看到代码运行结果。
public class Test
{
public static void main(String args[])
{
System.out.println("机器人启动");
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("再见"));
}
}