一个简单的Socket通信Demo

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

⼀个简单的Socket通信Demo
服务器端Demo:
Server.java(服务器端运⾏主程序,直接运⾏):
package cn.wjs;
import .InetAddress;
import .ServerSocket;
import .Socket;
public class Server {
private static final int port = 9999;
private static final int backlog = 100;
private static final String bindAddr = "127.0.0.1";
public static void main(String[] args) throws Exception {
System.out.println("正在启动Socket服务器");
ServerSocket server = new ServerSocket(9998, 100, InetAddress.getByName("127.0.0.1"));
System.out.println("启动完成\n正在监听客户端连接");
while (true) {
Socket socket = server.accept();
System.out.println("产⽣了⼀个新的会话,来⾃:" + socket.getInetAddress() + "\n=================================="); new ServerMessageReceiver(socket).start();
new ServerMessageSender(socket).start();
System.out.println("服务器端已启动");
}
}
}
ServerMessageReceiver.java:
package cn.wjs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import .Socket;
public class ServerMessageReceiver extends Thread
{
private Socket socket;
public ServerMessageReceiver(Socket socket)
{
this.socket = socket;
}
public void run() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), "UTF-8"));
String content = null;
while (true) {
if (this.socket.isClosed()) {
System.out.println("Socket已经关闭");
reader.close();
this.socket.close();
break;
}
content = reader.readLine();
if (content.equals("bye")) {
System.out.println("对⽅请求关闭连接,⽆法继续进⾏聊天");
reader.close();
this.socket.close();
break;
}
System.out.println("【对⽅】" + content + "\n");
}
reader.close();
this.socket.close();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
ServerMessageSender.java:
package cn.wjs;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import .Socket;
public class ServerMessageSender extends Thread
{
private Socket socket;
public ServerMessageSender(Socket socket)
{
this.socket = socket;
}
public void run() {
try {
BufferedWriter outwriter = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream(), "UTF-8")); BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
String msg = null;
while (true) {
msg = inputReader.readLine();
if (msg.toLowerCase().equals("exit")) {
System.exit(0);
}
if (this.socket.isClosed()) {
System.out.println("Socket已经关闭");
outwriter.close();
inputReader.close();
this.socket.close();
break;
}
outwriter.write(msg);
outwriter.newLine();
outwriter.flush();
}
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
ClientDemo:
Client.java(客户端主程序,直接运⾏):
package cn.wjs;
import java.io.IOException;
import .InetAddress;
import .Socket;
import .UnknownHostException;
public class Client
{
private static final int port = 9999;
private static final String bindAddr = "127.0.0.1";
public static void main(String[] args)
{
System.out.println("正在连接Socket服务器");
try {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9998);
System.out.println("已连接\n================");
new ClientMessageSender(socket).start();
new ClientMessageReceiver(socket).start();
System.out.println("已启动");
}
catch (UnknownHostException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
ClientMessageReceiver.java:
package cn.wjs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import .Socket;
public class ClientMessageReceiver extends Thread
{
private Socket socket;
public ClientMessageReceiver(Socket socket)
{
this.socket = socket;
}
public void run()
{
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), "UTF-8")); String content = null;
while (true) {
if (this.socket.isClosed()) {
System.out.println("Socket已经关闭");
reader.close();
this.socket.close();
break;
}
content = reader.readLine();
if (content.equals("bye")) {
System.out.println("对⽅请求关闭连接,⽆法继续进⾏聊天");
reader.close();
this.socket.close();
break;
}
System.out.println("【对⽅】" + content + "\n");
}
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
ClientMessageSender.java:
package cn.wjs;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import .Socket;
public class ClientMessageSender extends Thread
{
private Socket socket;
public ClientMessageSender(Socket socket)
{
this.socket = socket;
}
public void run()
{
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream(), "UTF-8"));
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String msg = null;
while (true) {
msg = reader.readLine();
if (msg.toLowerCase().equals("exit")) {
System.exit(0);
}
if (this.socket.isClosed()) {
System.out.println("Socket已经关闭");
writer.close();
this.socket.close();
break;
}
writer.write(msg);
writer.newLine();
writer.flush();
}
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
通过elicpse导出jar包直接运⾏,服务器端导出jar设置main.class为Server.java,客户端导出jar设置main.class为Client.java
------
这是传统的TCP/BIO模式.如果多个客户端向服务器发送请求,通常采⽤连接池的⽅式来维护,这和数据库的连接池⼀个道理。

当然,线程池中socket的数量有限,同时使⽤socket请求有可能造成客户端出现⽆限的等待,可以通过socket.setSoTimeout设置响应超时时间。

相关文档
最新文档