导航:首页 > 编程语言 > 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编程多线程相关的资料

热点内容
正版我的世界如何进服务器地址 浏览:658
云文档怎样加密 浏览:294
ip协议的远程登录命令 浏览:286
阿里云服务器可以帮别人备案吗 浏览:391
脏数据java 浏览:290
游戏解压怎么设置 浏览:782
会声会影如何压缩视频 浏览:57
阅读app小说怎么转换成txt 浏览:65
c语言编程数字变时间 浏览:655
迷你编程第五天初级宝箱怎么弄 浏览:839
刺激体验服如何更新服务器 浏览:934
怎么把照片做成新的文件夹 浏览:466
安卓手机没有声音均衡器怎么办 浏览:506
吃鸡国际服为什么会服务器匆忙 浏览:248
微信中如何打开定位服务器 浏览:203
java并发编程书籍 浏览:280
android601源码 浏览:788
程序员离职了还能干嘛 浏览:156
少林功法pdf 浏览:471
安卓80版本小游戏怎么玩 浏览:632