socket ( java ) 简单客户端、服务端通信
程序员文章站
2024-03-23 09:48:46
...
原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。否则将追究法律责任。http://blog.csdn.net/love254443233/article/details/7881886
实现:
客户端:用两个socket(两个端口端口),一个用于接收服务端发送过来的消息,一个用于向服务端发送消息。
服务端:用两个socket,一个用于接收客户端发送过来的消息,一个用于向客户端发送消息。
注:可单独运行SocketServer与SocketClient;
1、SocketServer类:
package org.jivesoftware.openfire.plugin.test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocketServer implements Runnable {
public int hostPort = 5000;
public int remotePort = 5001;
public boolean sendMessage=false;
public String remoteAddress="localhost";
public static void main(String args[]){
SocketServer socketServer=new SocketServer();
socketServer.run();
}
public void run() {
Socket socket=null;
ServerSocket server=null;
InputStream is=null;
OutputStream os=null;
try {
server = new ServerSocket(hostPort);
socket = server.accept();
System.out.println(socket);
is = socket.getInputStream();
byte[] bytes;
while (true) {
bytes = new byte[2048];
int n = is.read(bytes);
System.out.print(new String(bytes, 0, n));
if(sendMessage)
sendMessage();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (socket != null)
socket.close();
if(is!=null)
is.close();
if(os!=null)
os.close();
if(server!=null)
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessage() {
Socket socket = null;
OutputStream outputStream = null;
int i = 0;
String string = "这里是服务器。。。";
try {
socket = new Socket(remoteAddress, remotePort);
outputStream = socket.getOutputStream();
outputStream.write((string + (i++)+"\n").getBytes());
outputStream.flush();
} catch (UnknownHostException e1) {
try {
if (socket != null)
socket.close();
if (outputStream != null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("服务器端口没开。。");
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
2、SocketClient类:
package org.jivesoftware.openfire.plugin.test;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
public class SocketClient implements Runnable {
public static void main(String args[]) {
SocketClient socketClient = new SocketClient();
socketClient.run();
}
public int remotePort = 5000;
public String remoteAddress = "localhost";
public SocketClient() {
}
public void run() {
Socket socket = null;
OutputStream outputStream = null;
try {
socket = new Socket(remoteAddress, remotePort);
outputStream = socket.getOutputStream();
outputStream.write(("connect to the server at time " + new Date().toString()+"\n").getBytes());
outputStream.flush();
System.out.println(socket);
byte[] buffer;
int size = 0;
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();
}
try {
if (socket != null)
socket.close();
if (outputStream != null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、TCPServer类:
package org.jivesoftware.openfire.plugin.test;
public class TCPServer {
private static SocketServer socketServer;
public static void main(String[] args){
socketServer = new SocketServer();
socketServer.hostPort=5000;
socketServer.remotePort=5001;
socketServer.sendMessage=true;
Thread serverThread = new Thread(socketServer);
/*socketClient = new SocketClient();
socketClient.remotePort=5001;
//socketClient.setStr("TCPServer:");
Thread clienThread = new Thread(socketClient);
clienThread.start(); */
serverThread.start();
System.out.println("服务器启动完成:");
}
}
4、TCPClent类:
package org.jivesoftware.openfire.plugin.test;
public class TCPClent {
private static SocketServer socketServer;
private static SocketClient socketClient;
public static void main(String[] args) {
socketServer = new SocketServer();
socketServer.hostPort=5001;
socketServer.remotePort=5000;
socketServer.sendMessage=true;
Thread serverThread = new Thread(socketServer);
socketClient = new SocketClient();
socketClient.remotePort=5000;
Thread clienThread = new Thread(socketClient);
clienThread.start();
serverThread.start();
System.out.println("服务器启动完成:");
}
}