導航:首頁 > 編程語言 > csocket編程多線程

csocket編程多線程

發布時間:2023-05-24 21:34:26

Ⅰ 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();
}
}
}
}

閱讀全文

與csocket編程多線程相關的資料

熱點內容
臟數據java 瀏覽:290
游戲解壓怎麼設置 瀏覽:782
會聲會影如何壓縮視頻 瀏覽:57
閱讀app小說怎麼轉換成txt 瀏覽:65
c語言編程數字變時間 瀏覽:655
迷你編程第五天初級寶箱怎麼弄 瀏覽:839
刺激體驗服如何更新伺服器 瀏覽:934
怎麼把照片做成新的文件夾 瀏覽:466
安卓手機沒有聲音均衡器怎麼辦 瀏覽:506
吃雞國際服為什麼會伺服器匆忙 瀏覽:248
微信中如何打開定位伺服器 瀏覽:203
java並發編程書籍 瀏覽:280
android601源碼 瀏覽:788
程序員離職了還能幹嘛 瀏覽:156
少林功法pdf 瀏覽:471
安卓80版本小游戲怎麼玩 瀏覽:632
奇書pdf 瀏覽:836
伺服器的管理口有什麼用 瀏覽:643
澳洲加密資產新政策 瀏覽:157
哈利波特連接伺服器失敗什麼意思 瀏覽:234