1.多线程方式循环处理多个socket请求-server
server
package com.sunyj.practice.t0001_socket.multiThread;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 多线程方式循环处理多个socket请求-server
* @author syj
* @date 2019年5月15日 下午9:52:16
*/
public class Server {
public static void main(String[] args) {
int port = 9212;
try {
@SuppressWarnings("resource")
ServerSocket serverSocket = new ServerSocket(port);
// serverSocket.setSoTimeout();
System.out.println("server一直等待client连接的到来...");
ExecutorService executorService = Executors.newFixedThreadPool(100);
while(true) {
Socket socket = serverSocket.accept();
Runnable runnable = () -> {
readMsg(socket);
};
executorService.submit(runnable);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取socket传来的信息
* @param socket
* @throws IOException
* @throws UnsupportedEncodingException
*/
private static void readMsg(Socket socket){
try {
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len;
StringBuilder sb = new StringBuilder();
while((len = inputStream.read(bytes)) != -1) {
sb.append(new String(bytes,0,len,"utf-8"));
}
System.out.println(sb.toString());
inputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.可重复发送信息SOCKET - client
client
package com.sunyj.practice.t0001_socket.repeatSend;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* 可重复发送信息SOCKET - client
* @author syj
* @date 2019年5月15日 下午9:17:45
*/
public class Client {
public static void main(String[] args) throws UnknownHostException,IOException {
String host = "127.0.0.1";
int port = 9212;
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(host,port), 10000);
OutputStream outputStream = socket.getOutputStream();
String msg = "你好,12312sfsaf";
byte[] bytes = msg.getBytes("utf-8");
outputStream.write(bytes.length >> 8);
System.out.println(bytes.length);
System.out.println(bytes.length >> 8);
outputStream.write(bytes.length);
outputStream.write(msg.getBytes("utf-8"));
outputStream.flush();
msg = "second send message...";
outputStream.write(bytes.length >> 8);
outputStream.write(bytes.length);
outputStream.write(msg.getBytes("utf-8"));
outputStream.flush();
msg = "第三次发送...";
outputStream.write(bytes.length >> 8);
outputStream.write(bytes.length);
outputStream.write(msg.getBytes("utf-8"));
outputStream.flush();
outputStream.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
throw e;
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
}
server
package com.sunyj.practice.t0001_socket.repeatSend;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 可重复发送的SOCKET
* @author syj
* @date 2019年5月15日 下午9:06:52
*/
public class Server {
public static void main(String[] args) throws IOException {
int port = 9212;
try {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("-----server一直等待客户端连接的到来...");
serverSocket.setSoTimeout(10000);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
byte[] bytes;
while(true) {
int first = inputStream.read(); //读取第一个字节
if(first == -1) {
break;
}
int second = inputStream.read();
System.out.println(first << 8);
int len = (first << 8) + second;
System.out.println(len);
bytes = new byte[len];
inputStream.read(bytes, 0, len);
System.out.println("------get message from client:"+new String(bytes,"utf-8"));
}
inputStream.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
}
3.simple
client
package com.sunyj.practice.t0001_socket.simple;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
/**
*
* @author syj
* @date 2019年5月15日 下午8:24:18
*/
public class Client {
public static void main(String[] args) {
String host = "127.0.0.1";
int port = 9212;
try {
Socket socket = new Socket(host,port);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("你好".getBytes("utf-8"));
//通过shutdownoutput来告诉服务器已经发送完数据,后续只能接受数据
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len;
StringBuilder sb = new StringBuilder();
while((len = inputStream.read(bytes)) != -1) {
sb.append(new String(bytes,0,len,"UTF-8"));
}
System.out.println("get message from server:"+sb.toString());
inputStream.close();
outputStream.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
server
package com.sunyj.practice.t0001_socket.simple;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* socket服务端
* @author syj
* @date 2019年5月15日 下午8:00:49
*/
public class Server {
public static void main(String[] args) {
int port = 9212;
try {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("server一直等待客户端连接的到来。。。");
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream(); //读取到内存中
byte[] bytes = new byte[1024];
StringBuilder sb = new StringBuilder();
int len;
while((len = inputStream.read(bytes)) != -1) {
sb.append(new String(bytes,0,len,"UTF-8"));
}
System.out.println("get message from client:"+sb.toString());
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello client,I get the message.".getBytes("UTF-8"));
outputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}