Ⅰ socket一個服務端多個客戶端,多線程這么實現
2種方式,一種:你處理客戶端的連接,開一個子線程(需要注意:控制線程個數),第二種:非同步掛起處理、
Ⅱ c++ 多線程socket編程可以多個線程同時accept數據嗎
你別多個線程在同一個socket上調用accept就成。
多個線程在同一個socket上調用accept我襲唯真不知道它會有什麼樣的行為
常見的一種做法是在一個socket上調用accept,接受連接以拍彎培後鬧搭丟給某個線程去完成接下來的事情
Ⅲ socket java實現客戶端多線程接受消息並發送消息給伺服器,並發執行
客服端:
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);
KeFuanJie kf = new KeFuanJie(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 KeFuanJie extends Thread{
Socket soc;
String ss;
BufferedReader br;
public KeFuanJie(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();
}
}
}
}