Socket 简介:
网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket。
建立网络通信连接至少要一对端口号(socket)。socket本质是编程接口(API),对TCP/IP的封装,TCP/IP也要提供可供程序员做网络开发所用的接口,这就是Socket编程接口;HTTP是轿车,提供了封装或者显示数据的具体形式;Socket是发动机,提供了网络通信的能力。
Socket的英文原义是“孔”或“插座”。作为BSD UNIX的进程通信机制,取后一种意思。通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,可以用来实现不同虚拟机或不同计算机之间的通信。在Internet上的主机一般运行了多个服务软件,同时提供几种服务。每种服务都打开一个Socket,并绑定到一个端口上,不同的端口对应于不同的服务。Socket正如其英文原义那样,像一个多孔插座。一台主机犹如布满各种插座的房间,每个插座有一个编号,有的插座提供220伏交流电, 有的提供110伏交流电,有的则提供有线电视节目。 客户软件将插头插到不同编号的插座,就可以得到不同的服务。
Socket服务端示例代码
package com.tencent.AbcBank.util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
/**
* Socket Server
*
* @author guoqing
*
*/
@Slf4j
@Component
public class SocketServer {
@Value("${serverPort}")
private static int serverPort;
public static void main(String[] args) {
server();
}
public static void server() {
StringBuilder sb = new StringBuilder("");
InputStream is = null;
OutputStream os = null;
InputStreamReader isr = null;
BufferedReader br = null;
ServerSocket server = null;
PrintWriter out = null;
Socket socket = null;
try {
server = new ServerSocket(serverPort);
log.info("Socket Server Starting ····· ");
socket = server.accept();
// 输入流,接收客户端发送的数据
is = socket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String temp = null;
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
log.info("Server Recive Message is :" + sb.toString());
// 输出流,向客户端返回数据
os = socket.getOutputStream();
out = new PrintWriter(os);
out.println(" Your message are " + sb.toString());
out.flush();
socket.shutdownOutput();
} catch (Exception e) {
log.error("Socket Server Errro Message is " + e.getLocalizedMessage());
e.printStackTrace();
} finally {
try {
if (null != out) {
out.close();
}
if (null != os) {
os.close();
}
if (null != br) {
is.close();
}
if (null != isr) {
isr.close();
}
if (null != is) {
br.close();
}
if(null!=socket) {
socket.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
Socket客户端示例代码
package com.tencent.AbcBank.util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
/**
* Socket Client
*
* @author guoqing
*
*/
@Slf4j
@Component
public class SocketClient {
@Value("${serverIp}")
private static String serverIp;
@Value("${serverPort}")
private static int serverPort;
public static void main(String[] args) {
connectionToServer();
}
/**
* Connection to Socket Server
*
* @return
*/
public static String connectionToServer() {
StringBuilder sb = new StringBuilder();
Socket socket = null;
OutputStream os = null;
PrintWriter out = null;
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
socket = new Socket(serverIp, serverPort);
log.info(" connection to Server ");
// 输出流,输出数据到服务端
os = socket.getOutputStream();
out = new PrintWriter(os);
out.print("Hello World !");
out.flush();
socket.shutdownOutput();
// 输入流,接收服务器端返回的信息
is = socket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String temp = "";
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
log.info("recive Server Message is " + sb.toString());
} catch (Exception e) {
log.error("Socket Client Error , Message is " + e.getLocalizedMessage());
e.printStackTrace();
} finally {
try {
if (null != br) {
br.close();
}
if (null != isr) {
isr.close();
}
if (null != is) {
is.close();
}
if (null != out) {
out.close();
}
if (null != os) {
os.close();
}
if (null != socket) {
socket.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return sb.toString();
}
}
控制台输出内容:
客户端:
服务端: