您的位置:首页 > 编程语言 > Java开发

java socket编程多线程处理多客户端

2017-04-08 13:45 615 查看
一、服务器端启动程序

package com.chhuang.main;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import com.chhuang.thread.AcceptRunner;

public class App {

public static final int serverPort = 8808;
public static ServerSocket serverSocket = null;

public static void main(String[] args) {
try {
serverSocket = new ServerSocket(serverPort);
System.out.println("\n服务器已启动");
System.out.println("服务器IP:"+InetAddress.getLocalHost().getHostAddress());
System.out.println("端口号:"+serverPort);
System.out.println("等待客户端登录......\n");
//接收客户端连接
while(true){
Socket clientSocket = serverSocket.accept();
String clientIp = clientSocket.getInetAddress().getHostAddress();//读取客户端ip
int clientPort = clientSocket.getPort();//读取客户端端口号
System.out.println("\n客户端"+clientIp+":"+clientPort+"上线\n");
//服务器为客户端打开线程接收客户端数据
new Thread(new AcceptRunner(clientSocket)).start();

//测试
test(clientSocket);

}
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 测试
* @param clientSocket
* @throws IOException
*/
private static void test(Socket clientSocket) throws IOException{
//测试
for(int i=0; i<10; i++){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(!clientSocket.isClosed()){
PrintWriter out=new PrintWriter(clientSocket.getOutputStream(),true);
out.println("hi"+i);
}else{
break;
}
}
}
}


二、服务器端接收客户端发送信息的线程

package com.chhuang.thread;

import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class AcceptRunner implements Runnable {

private Socket clientSocket;

public AcceptRunner(Socket clientSocket){
super();
this.clientSocket = clientSocket;
}

@Override
public void run() {
if(clientSocket==null){
return;
}
Scanner in = null;
try {
in = new Scanner(clientSocket.getInputStream());
while(in.hasNextLine()){
String content = in.nextLine();
System.out.println(content);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(in!=null) in.close();
String clientIp = clientSocket.getInetAddress().getHostAddress();//读取客户端ip
int clientPort = clientSocket.getPort();//读取客户端端口号
System.out.println("\n客户端"+clientIp+":"+clientPort+"已离线\n");
}
}

}


三、客户端启动程序

package com.chhuang.main;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import com.chhuang.thread.AcceptRunner;

public class App {

public static final String serverIp = "192.168.1.100";
public static final int serverPort = 8808;

public static void main(String[] args) {
try {
Socket clientSocket = new Socket(serverIp, serverPort);
String ip = clientSocket.getInetAddress().getHostAddress();
int port = clientSocket.getLocalPort();
System.out.println("客户端"+ip+":"+port+"启动成功");
//接收服务器发来的信息
new Thread(new AcceptRunner(clientSocket)).start();
} catch (UnknownHostException e) {
System.out.println("服务器已停止运行");
e.printStackTrace();
} catch (IOException e) {
System.out.println("服务器已停止运行");
e.printStackTrace();
}
}

}


四、客户端接收服务器发送数据的线程

package com.chhuang.thread;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class AcceptRunner implements Runnable {

private Socket clientSocket;

public AcceptRunner(Socket clientSocket){
super();
this.clientSocket = clientSocket;
}

@Override
public void run() {
if(clientSocket==null){
return;
}
Scanner in = null;
try {
in=new Scanner(clientSocket.getInputStream());
while(in.hasNextLine()){
String content = in.nextLine();
System.out.println(content);

//接收到hi,回复hello
if(content!=null && content.startsWith("hi")){
//向服务器发送
PrintWriter out=new PrintWriter(clientSocket.getOutputStream(),true);
out.println("hello "+content);
}

}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(in!=null) in.close();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息