Java:Socket通信

来源:互联网 发布:steam饥荒mac怎么汉化 编辑:程序博客网 时间:2024/05/24 01:51

    Socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求。ServerSocket用于服务器端,Socket是建立网络连接时使用的。在连接成功时,应用程序两端都会产生一个Socket实例,操作这个实例,完成所需的会话。对于一个网络连接来说,套接字是平等的,并没有差别,不因为在服务器端或在客户端而产生不同级别。套接字之间的连接过程可以分为三个步骤:服务器监听,客户端请求,连接确认。

实例一:基于TCP/IP协议的Socket通信一对一:

package z_test_Socket_Demo;import java.io.IOException;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class TestServerSocket {public static void main(String[] args) {// TODO 自动生成的方法存根ServerSocket serverSocket = null;Socket socket = null;OutputStream outputStream = null;try {serverSocket = new ServerSocket(8888); // 指定监听端口System.out.println("==>> accepting");socket = serverSocket.accept(); // 监听客户端的请求,方法阻塞,请求成功,返回socket对象outputStream = socket.getOutputStream(); // 获得输出流String string = "==>> Hello client,This is a message from server"; // 随便给客户端发点东西byte[] buffer = string.getBytes();outputStream.write(buffer);outputStream.flush();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();} finally {try {serverSocket.close();socket.close();outputStream.close();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}}}}
package z_test_Socket_Demo;import java.io.IOException;import java.io.InputStream;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;public class TestClientSocket {public static void main(String[] args) {// TODO 自动生成的方法存根Socket socket = null;InputStream inputStream = null;try {// socket = new Socket(InetAddress.getLocalHost(), 8888);// 请求连接服务器的8888端口,构造方法通常可以指定IP地址socket = new Socket("10.25.26.141", 8888);inputStream = socket.getInputStream(); // 获得输入流byte[] buffer = new byte[64];int temp = 0;while ((temp = inputStream.read(buffer)) != -1) { // 将数据读入字节数组,此方法阻塞System.out.println(new String(buffer));}} catch (UnknownHostException e) {// TODO 自动生成的 catch 块e.printStackTrace();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();} finally {try {socket.close();inputStream.close();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}}}}
先运行服务器端,打印结果:

==>> accepting
然后运行客户端,打印结果:

==>> Hello client,This is a message from server

实例二:基于TCP/IP协议的Socket通信一对多:

只需要把服务器段用一个线程死循环即可

package z_test_Socket_Demo;import java.io.IOException;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class TestServerSocket {public static void main(String[] args) {// TODO 自动生成的方法存根new Thread(new ServerThread()).start();}private static class ServerThread implements Runnable {@Overridepublic void run() {// TODO 自动生成的方法存根int i = 1;while (true) {ServerSocket serverSocket = null;Socket socket = null;OutputStream outputStream = null;try {serverSocket = new ServerSocket(8888); // 指定监听端口System.out.println("==>> accepting client" + i++);socket = serverSocket.accept(); // 监听客户端的请求,方法阻塞,请求成功,返回socket对象outputStream = socket.getOutputStream(); // 获得输出流String string = "==>> Hello client" + i+++ ",This is a message from server"; // 随便给客户端发点东西byte[] buffer = string.getBytes();outputStream.write(buffer);outputStream.flush();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();} finally {try {serverSocket.close();socket.close();outputStream.close();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}}}}}}
运行服务器段,打印结果:

==>> accepting client1

运行客户端,客户端打印结果:

==>> Hello client1,This is a message from server

再次运行客户端,客户端打印结果:

==>> Hello client2,This is a message from server

相当于两个不同的客户端请求连接服务器


    弄懂的了基于TCP/IP协议的Socket通信,基于其他协议的Socket通信应该可以很快就能弄明白了。


0 1