Java:Socket通信

    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 {

		@Override
		public 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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值