⼀个简单的Socket通信Demo
服务器端Demo:
Server.java(服务器端运⾏主程序,直接运⾏):
package cn.wjs;
import java.InetAddress;
import java.ServerSocket;
import java.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, ByName("127.0.0.1"));
System.out.println("启动完成\n正在监听客户端连接");
while (true) {
Socket socket = server.accept();
System.out.println("产⽣了⼀个新的会话,来⾃:" + InetAddress() + "\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 java.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(InputStream(), "UTF-8"));
String content = null;
while (true) {
if (this.socket.isClosed()) {
System.out.println("Socket已经关闭");
reader.close();
this.socket.close();
break;
}
content = adLine();
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 java.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(OutputStream(),
"UTF-8")); BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
String msg = null;
while (true) {
msg = adLine();
if (LowerCase().equals("exit")) {
}
if (this.socket.isClosed()) {
System.out.println("Socket已经关闭");
outwriter.close();
inputReader.close();
this.socket.close();
break;
}
outwriter.write(msg);
outwriter.flush();
}
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
ClientDemo:
Client.java(客户端主程序,直接运⾏):
package cn.wjs;
import java.io.IOException;
import java.InetAddress;
import java.Socket;
import java.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 ByName("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();
}
socket通信在哪一层
}
}
ClientMessageReceiver.java:
package cn.wjs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.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(InputStream(), "UTF-8")); String content = null;
while (true) {
if (this.socket.isClosed()) {
System.out.println("Socket已经关闭");
reader.close();
this.socket.close();
break;
}
content = adLine();
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 java.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(OutputStream(), "UTF-8"));
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String msg = null;
while (true) {
msg = adLine();
if (LowerCase().equals("exit")) {
}
if (this.socket.isClosed()) {
System.out.println("Socket已经关闭");
writer.close();
this.socket.close();
break;
}
writer.write(msg);
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设置响应超时时间
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论