在网上看了很多关于Thread和Socket的资料,项目中也用到不少,但是都是比较简单,或者是用一些现成的,现在想从头开始,慢慢的再学习。希望能和各位前辈进行交流。接触的需求好多一开始比较简单,后来就慢慢复杂了,我想这个学的过程也是,先来个简单的,然后再慢慢的进行完善。好了,不废话了。
场景:客户端将键盘输入的字符通过socket发送到服务端,当输入的字符为“exit”时,程序退出,服务端仅仅将接收到的信息打印出来。
服务端代码:
public class SocketServer extends Thread{
private java.net.ServerSocket ss;
private java.net.Socket s;
@Override
public void run() {
try{
while(true){
this.s=this.ss.accept();
java.io.BufferedReader reader =
new BufferedReader(
new java.io.InputStreamReader(
s.getInputStream()));
String str=reader.readLine();
System.out.println("开始接受数据:");
while(str!=null){
System.out.println("读了一行");
System.out.println(str);
str=reader.readLine();
}
reader.close();
this.s.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
public SocketServer(int port){
try{
System.out.println("服务端启动.......");
this.ss=new ServerSocket(port);
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args){
Thread t=new SocketServer(8888);
t.start();
}
}
客户端的代码:
public class SocketClient{
public static void main(String[] args)throws Exception {
java.net.Socket s=new java.net.Socket("127.0.0.1",8888);
java.io.BufferedWriter out=new java.io.BufferedWriter(
new java.io.OutputStreamWriter(
new java.io.BufferedOutputStream(s.getOutputStream())));
java.io.BufferedReader reader=new BufferedReader(
new java.io.InputStreamReader(System.in));
String str=reader.readLine();
while(!"exit".equals(str)){
out.write(str + "\n");
out.flush();
str=reader.readLine();
}
reader.close();
out.close();
}
}
分享到:
相关推荐
socket thread操作 socket thread操作 socket thread操作 自己还没看哦
SocketThread实例(SocketThread实例.txt)
socket 接收多个终端上传的数据 ,每个终端新建一个socket,超时的socket定时清理,防止连接过多影响服务器性能
Socket Programming & Thread Pool实例
用javaSwing+Socket+Thread实现的群聊程序,分为服务器和客户端,没用到数据库
Socket与Thread扫描端口,简单代码,初级运用,初学者。
这是一个java Socket Thread Io 大文件下载上传的整合
Socket简单通讯 //声明将要用到的类 private IPEndPoint ServerInfo;//存放服务器的IP和端口信息 private Socket ServerSocket;//服务端运行的SOCKET private Thread ServerThread;//服务端运行的线程 private...
提供了许多线程间通信的实例程序。用linux下的C语言实现。-Provides many examples of thread communication procedures. Under linux using C language.
socket通讯,支持多线程通讯Socket communication, multi-threaded TCPIP communication, heartbeat maintenance, automatic reconnection, thread number configuration, etc. 报错 双语对照 Socket ...
import socket,time,SocketServer,struct,os,thread\nhost='192.168.50.74' port=12307 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #定义socket类型 s.bind((host,port)) #绑定需要监听的I
protected Socket socket; protected ObjectInputStream dataIn; protected ObjectOutputStream dataOut; protected Thread listener; protected static Vector handlers = new Vector(); private boolean ...
PyQt5结合Socket聊天室功能开发,项目中使用线程进行不同任务处理,是学习Scoket及线程非常好的案例。
这个是socket网络发送线程。使用这个线程,可以并行运行。有效的减小了时间,提高了发送的速率。
socket多线程thread通讯.rar
在 Java 中,多线程可以使用 Thread 类和 Runnable 接口来实现,而 Socket 编程则需要使用 java.net 包中的相关类来实现。 在 Java 多线程-Socket 编程中,客户端和服务器端都需要使用 Socket 来进行通信。客户端...
Socket socket = accept(); while (true) { new ServerReaderThread(socket); new ServerWriterThread(socket); } } finally { close(); } } public static void main(String[] args) ...
Full Multi-thread Client_Server Socket Class with ThreadPoolSocketPort_Linux.zip
Android 通过Socket 和服务器通讯,是一...(4)构建 SocketThreadManager对以上三个thread进行管理 (5)构建 TCPClient 发送socket消息 在NIO的方式实现TCP,特别是在接收服务器的数据,不用写个线程定时去读了。
thread thread thread thread thread thread thread thread thread thread thread thread thread thread thread thread thread thread thread thread thread thread thread thread thread thread thread thread ...