socket ( java ) 简单多个客户端、服务端通信(多线程)

时间:2021-12-28 00:50:34
原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处  、作者信息和本声明。否则将追究法律责任。 http://blog.csdn.net/love254443233/article/details/7897269


实现:

客户端:多个socket(多个端口),其中一个客户端的一个端口用于接收服务端发送过来的消息,其一个用于向服务端发送消息。其它客户端只有发消息的功能。

服务端:两个socket,一个用于循环接收客户端发送过来的socket请求。一个用于接收消息手自动向客户端发送消息。

注:先运行MySocketServer,然后MySocketClient,最后SocketClient;MySocketClient、SocketClient向MySocketServer发送消息。

1、MySocketServer类:

package socket._5;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MySocketServer {
private static int localHostPort = 5000;

public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(localHostPort);
System.out.println("服务器端------监听中.....");
while (true) {
Socket socket = s.accept();
System.out.println("接入的socket:" + socket);
GetMessage getMessage = new GetMessage(socket);
Thread thread = new Thread(getMessage);
thread.start();
}
}
}

// 接收消息
class GetMessage implements Runnable {
private int remotePort = 5001;
private String remoetAddress = "localhost";
private InputStream inputStream;
private OutputStream outputStream;
private Socket socketGet;
private Socket socketSendMessage;
private boolean socketIsExits = false;
private int sum = 0;

private byte[] buffer;

public GetMessage(Socket socket) {
this.socketGet = socket;
try {
inputStream = socketGet.getInputStream();
outputStream = socketGet.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}

public void run() {
String str = "";
int n = 0;
while (true) {
try {
buffer = new byte[2048];
n = inputStream.read(buffer);
str = new String(buffer, 0, n);
System.out.print("客户端:" + str);
sendMessage();
} catch (IOException e) {
e.printStackTrace();
break;
}
if (str.equals("q")) {
break;
}
}
try {
if (socketGet != null)
socketGet.close();
if (inputStream != null)
inputStream.close();
} catch (Exception e) {

}
}

// 发送消息
public void sendMessage() throws IOException {
if (socketIsExits) {
try {
String input = "======" + (sum++);
System.out.println("服务端发送 socket:" + this.socketSendMessage);
outputStream.write(input.getBytes());
System.out.println("服务器:" + input);
outputStream.flush();
} catch (Exception e) {
System.out.println("客户端socket不存在。");
checkSocket();
}
} else {
checkSocket();
}
}

private void checkSocket() {
try {
socketSendMessage = new Socket(remoetAddress, remotePort);
outputStream = socketSendMessage.getOutputStream();
socketIsExits = true;
} catch (Exception e) {
socketIsExits = false;
}
}
}
2、MySocketClient类:

package socket._5;

import java.io.*;
import java.net.*;

public class MySocketClient {
private static int localHostPort = 5001;

public static void main(String[] args) throws IOException {

CSendMessage cSendMessage = new CSendMessage();
Thread thread2 = new Thread(cSendMessage);
thread2.start();

ServerSocket serverSocket = new ServerSocket(localHostPort);
while (true) {
Socket socketServer = serverSocket.accept();
CGetMessage getMessage = new CGetMessage(socketServer);
Thread thread = new Thread(getMessage);
thread.start();
}
}
}

// 接收消息
class CGetMessage implements Runnable {
private InputStream inputStream;
private Socket socket;
private byte[] buffer;

public CGetMessage(Socket socket) {
this.socket = socket;
try {
inputStream = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
}

public void run() {
String str = "";
while (true) {
buffer = new byte[2048];
int n = 0;
try {
n = inputStream.read(buffer);
str = new String(buffer, 0, n);
System.out.println("服务器:" + str);
} catch (IOException e) {
e.printStackTrace();
break;
}
if (str.equals("q")) {
break;
}
}
try {
if (socket != null)
socket.close();
if (inputStream != null)
inputStream.close();
} catch (Exception e) {

}
}
}

// 发送消息
class CSendMessage implements Runnable {
private boolean socketIsExits = false;
private OutputStream outputStream;
private Socket socketClient;
private int remotePort = 5000;
private String remoteAddress = "localhost";
private byte[] buffer;

public CSendMessage() throws IOException {
}

public void run() {
String str = new String();
checkSocket();
int size = 0;
while (true) {
try {
System.out.println("请输入:");
buffer = new byte[2048];
size = System.in.read(buffer);
str = new String(buffer, 0, size);
} catch (IOException e) {
e.printStackTrace();
}
if (socketIsExits) {
try {
System.out.println("客户端发送 socket:" + this.socketClient);
System.out.print("客户端:" + str);
outputStream.write(str.getBytes());
outputStream.flush();
} catch (Exception e) {
System.out.println("客户端socket不存在。");
checkSocket();
}
} else {
checkSocket();
}
}
}

private void checkSocket() {
try {
socketClient = new Socket(remoteAddress, remotePort);
outputStream = socketClient.getOutputStream();
socketIsExits = true;
} catch (Exception e) {
socketIsExits = false;
}
}
}
3、SocketClient类:

package socket._5;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketClient implements Runnable {
public int remotePort = 5000;
public String remoteAddress = "localhost";


public static void main(String args[]) {
SocketClient socketClient = new SocketClient();
socketClient.run();
}


public SocketClient() {
}

public void run() {
Socket socket = null;
OutputStream outputStream = null;
byte[] buffer;
int size = 0;
try {
socket = new Socket(remoteAddress, remotePort);
outputStream = socket.getOutputStream();
System.out.println(socket);
while (true) {
System.out.println("input:");
buffer = new byte[2048];
size = System.in.read(buffer);
if (size > 0) {
outputStream.write(new String(buffer, 0, size).getBytes());
outputStream.flush();
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null)
socket.close();
if (outputStream != null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
}