标签:
了解Socket看这里:Socket是什么
编写Socket最简单的就是单线程的Socket,但基本上是没有实用意义的,因为在实际的应用中基本上是不止于一个Client的。一般都是多线程Socket程序。
了解多线程Socket看这里:[如何编写多线程Socket程序][3]
[3]:
要编写Socket,需要了解java.net包中提供了两个类Socket和ServerSocket,他们分别用来表示Socket的客户端和服务端。我们的代码也分为客户端和服务端两部分。
服务端代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketSimpleDemoServer {
private int port = 8000;// 端口
private ServerSocket serverSocket;
public SocketSimpleDemoServer() throws Exception {
serverSocket = new ServerSocket(port);
System.out.println("waitting connet...");
}
public void service() throws IOException {
Socket socket = null;
String msg = null;
while (true) {// 不停的监听,直到接收到请求
try {
socket = serverSocket.accept(); // 准备接受连接
System.out.println("new connection: " + socket.getInetAddress() + ":" + socket.getPort());
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")); // 输入流
PrintWriter writer = new PrintWriter(socket.getOutputStream()); // 输出流
while ((msg = reader.readLine()) != null) { // 接收消息
System.out.println("receive msg:" + msg);
writer.println(msg); // 发送消息
writer.flush(); // 注意,在使用缓冲流在发送消息的时候最好进行强制刷新,否则,可能会由于缓冲区不满而暂时不发送消息
if ("close".equals(msg)) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
}
}
}
public static void main(String[] args) throws Exception {
new SocketSimpleDemoServer().service();
}
}
运行服务端代码后,程序会一直进行监听,直到接收到客户端请求为止。结果如下:
waitting connet…
客户端代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class SocketDemoClient {
private String host = "127.0.0.1";// 要发送给服务端的ip
private int port = 8000;// 要发送给服务端的端口
private Socket socket;
public SocketDemoClient() throws Exception {
socket = new Socket(host, port);// 构造Socket客户端,并与连接服务端
}
public void talk() throws IOException {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
PrintWriter writer = new PrintWriter(socket.getOutputStream());
// 读取本地控制台的消息
BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in));
String msg = null;
while ((msg = localReader.readLine()) != null) {
writer.println(msg);
writer.flush();
System.out.println("send msg:" + reader.readLine());
if ("close".equals(msg)) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
}
}
public static void main(String[] args) throws Exception {
new SocketDemoClient().talk();
}
}
运行客户端代码后,我们查看服务端的Console,会出现以下结果,说明已经连接成功:
waitting connet…
new connection: /127.0.0.1:59349
我们在去客户端的Console中输入我们要发送的消息”维护世界和平”,回车确定后,客户端Console出现以下结果,消息已经发出:
send msg:维护世界和平
在服务端的Console中,我们会看到如下结果,说明消息已经被接受:
waitting connet…
new connection: /127.0.0.1:59349
receive msg:维护世界和平
这里要注意的是,在选择服务端口时,每一个端口提供一种特定的服务,端口不能冲突,包括系统保留的接口。通常0~1023的端口号为系统所保留,例如http服务的端口号为80,telnet服务的端口号为21,ftp服务的端口号为23。
Java千百问_02基本使用(011)_如何编写单线程Socket程序
标签:
原文地址:http://blog.csdn.net/ooppookid/article/details/51711164