Java socket编程(单线程socket,多线程socket)

xiaoxiao2021-02-28  13

Java实现单线程socket

思路

服务端:使用ServerSocket类建立服务端对象并监听端口,使用DataInputStream类.readUTF()方法获取客户端数据输入,关闭DataInputStream,释放socket。

客户端:使用Socket类建立客户端对象并发送连接请求,使用DataOutputStream类.writeUTF()方法向服务端发送数据,关闭DataOutputStream,释放socket。

效果

客户端发送一次数据并关闭,服务端接收一次数据并关闭

代码:

package socket; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * Created by 15114 on 2017/6/1. */ public class SocketService { public static void main(String[] args){ try { ServerSocket server = new ServerSocket(5555);//创建socket对象并监听5555端口 System.out.println("服务器启动成功\n"); Socket socket = server.accept();//等待客户端连接 System.out.println("socket连接成功!等待客户端输入\n"); DataInputStream in = new DataInputStream(socket.getInputStream());//创建DataInputStream对象接收数据流 String accpet = in.readUTF();//readUTF()接收UTF-8格式输入流 System.out.println("客户端说:"+ accpet);//输出接收到的数据 in.close();//释放in对象 socket.close();//关闭此socket server.close();//释放service对象 } catch (Exception e) { System.out.println("Error:" + e); } } }

客户端:

package socket; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.util.Scanner; /** * Created by 15114 on 2017/6/1. */ public class SocketClient { public static void main(String[] args) { try { Socket socket = new Socket("localhost", 5555); DataOutputStream out = new DataOutputStream(socket.getOutputStream()); Scanner scn = new Scanner(System.in); System.out.println("请输入发送给服务端的信息:"); String send = scn.nextLine(); out.writeUTF(send); System.out.println("发送成功!"); out.close(); socket.close(); } catch (IOException ex) { System.out.println("Error:" + ex); } } }

Java实现多线程socket

思路

大体与单线程相同,但多线程socket每次创建时需用Thread类start()方法创建一个线程对socket托管,使用循环控制实现多线程

效果

代码

服务端:

package threadsocket; import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; /** * Created by 15114 on 2017/6/1. */ public class Server { public static void main(String[] args) { try { ServerSocket serverSocket=new ServerSocket(8888); int count=0; System.out.println("***服务器即将启动,等待客户端的连接***"); while(true){ Socket socket=serverSocket.accept(); ThreadService serverThread=new ThreadService(socket); count++; System.out.println("终端" + count +"连接成功!"); InetAddress address=socket.getInetAddress(); System.out.println("当前客户端的IP:"+address.getHostAddress()); serverThread.start();//创建线程托管socket,继续循环创建新socket } } catch (IOException e) { System.out.println("Error:" + e); } } }

Thread类run方法重写:

package threadsocket; import java.io.*; import java.net.Socket; /** * Created by 15114 on 2017/6/1. */ public class ThreadService extends Thread{ Socket socket = null; public ThreadService(Socket socket) { this.socket = socket; } public void run(){ try { DataInputStream in = new DataInputStream(socket.getInputStream()); String accpet = in.readUTF(); System.out.println("终端说:" + accpet); in.close(); socket.close(); } catch (IOException e) { System.out.println("Error:" + e); } } }

客户端:

package threadsocket; import java.io.*; import java.net.Socket; import java.util.Scanner; /** * Created by 15114 on 2017/6/1. */ public class Client { //static private Random r = new Random(); public static void main(String[] args) { try { Socket socket = new Socket("localhost", 8888); System.out.println("终端 1 连接成功!\n"); DataOutputStream out = new DataOutputStream(socket.getOutputStream()); Scanner scn = new Scanner(System.in); System.out.println("请输入发送给服务端的信息:"); String send = scn.nextLine(); out.writeUTF(send); System.out.println("发送成功!"); out.close(); socket.close(); } catch (IOException ex) { System.out.println("Error:" + ex); } } }
转载请注明原文地址: https://www.6miu.com/read-1100106.html

最新回复(0)