socket java实现客户端多线程接受消息并发送消息给服务器,并发执行

2024-11-10 07:19:11
推荐回答(3个)
回答1:

客服端:
package MyKeFudaun;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class KeFuDuan {
public static void main(String[] args) {
KeFuDuan kf = new KeFuDuan();
kf.start();
}
public void start(){
Socket sco;
String ss= "";
try {
sco = new Socket("127.0.0.1",8866);
KeFuduanJie kf = new KeFuduanJie(sco);
KeFuWuFasong kfs = new KeFuWuFasong(sco);
kf.start();
kfs.start();

//sco.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

class KeFuduanJie extends Thread{

Socket soc;
String ss;
BufferedReader br;
public KeFuduanJie(Socket soc){
try {
this.soc = soc;
br = new BufferedReader(new InputStreamReader(soc.getInputStream()));

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

}

//负责接受服务端来的信息
public void run(){
while(true){
//接受服务器端来的信息
try {
ss = br.readLine();
System.out.println("服务器---->客服端: "+ss);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}

//向服务器发送东西
class KeFuWuFasong extends Thread{

Socket soc;
BufferedWriter bw;
BufferedReader brr;
public KeFuWuFasong(Socket soc){

this.soc = soc;

try {
brr =new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(soc.getOutputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void run(){
while(true){
//向服务器发送请求
try {
bw.write(brr.readLine());
bw.newLine();
bw.flush();// 或者用bw.close()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

服务器端:
package MyKeFudaun;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

public static void main(String[] args) {
Server server = new Server();
server.start();
}

public void start(){

try { //服务器端打开端口
ServerSocket server = new ServerSocket(4499);
Socket socket = null;
ServerToClientThread stct = null;

while(true){
socket = server.accept(); //迎接(接收)客户端的Socket访问
stct = new ServerToClientThread(socket); //分配一个新线程负责和信赖的Socket沟通
stct.start();
}

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

}

}

package MyKeFudaun;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class ServerReceiveFromClient extends Thread{

Socket socket;
BufferedReader br;
String s;

public ServerReceiveFromClient(Socket socket){
this.socket = socket;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}

public void run(){

while(true){
try {
s = br.readLine();
System.out.println(socket.getInetAddress().getHostAddress()+"发送了:"+s);

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

}
}
}

package MyKeFudaun;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class ServerToClientThread extends Thread{

Socket socket;
BufferedReader br;
BufferedWriter bw;
String s;

//建立的同时,和客户端的Socket建立输入、输出流
public ServerToClientThread(Socket socket){
this.socket = socket;
try {
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
}

}

public void run(){
ServerReceiveFromClient srfc = new ServerReceiveFromClient(socket);
srfc.start();

while(true){
try {
bw.write("欢迎光临。");
bw.newLine();
bw.flush();
Thread.sleep(10*1000);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

回答2:

对于通信来说,不存在绝对的服务器和客户端,谁在等待别人来,谁就是服务器,谁主动去联系人,谁就是客户端。
所以。
你要想客户端接受消息,那在启动客户端的时候,在客户端程序里开始一个提供端口的Socket就可以了。
ServerSocket serverSocket = new ServerSocket(5000);
while (true) {
final Socket socket = serverSocket.accept();
new Thread() {
Socket mySocket = socket;

@Override
public void run() {
try {
System.out.println(mySocket);
InputStream is = mySocket.getInputStream();
byte[] bytes = new byte[1024];
int n = is.read(bytes);
System.out.println(new String(bytes, 0, n));
OutputStream os = mySocket.getOutputStream();
os.write(("server reply at time " + new Date()
.toString()).getBytes());
mySocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}

回答3:

自己写的socket通信在遇到大的并发量时经常会出现各种各样的问题,对于你的这种需求,建议你使用apache下面的mina框架来写,很简单的,就是专门的网络通讯框架,里面的socket
封装的比较完善,没啥大问题!