SocketはTCPの長い接続の1を実現して、最も簡単なコード、InputStream、OutStream方式、コンソールはテストすることができます
4627 ワード
package TCP.LongConnection1;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class TCPServer {
private static ServerSocket serverSocket;
static {
try {
serverSocket = new ServerSocket(33321);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
while (true) {
try {
Socket socket = serverSocket.accept();
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
new Thread(() -> {
while (true) {
try {
System.out.println(dataInputStream.readUTF());
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
try {
dataOutputStream.writeUTF(scanner.next());
dataOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package TCP.LongConnection;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class TCPClient {
private static Socket socket;
static {
try {
socket = new Socket("127.0.0.1", 33321);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Thread(() -> {
while (true) {
try {
InputStream inputStream = socket.getInputStream();
if (inputStream.available() != 0) {
// inputStream.readAllBytes();
// System.out.println(new String(inputStream.readAllBytes()));
System.out.println(ByteUtil.switchInputStreamToString(inputStream));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String message = scanner.next();
try {
OutputStream outputStream = socket.getOutputStream();
outputStream.write(message.getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package LongConnection;
import java.io.IOException;
import java.io.InputStream;
public class ByteUtil {
private static final int BUFFER_SIZE = 1024;
public static String switchInputStreamToString(InputStream inputStream) throws IOException {
byte[] bytes = new byte[BUFFER_SIZE];
byte[] totalBytes = new byte[0];
int inputStreamBytesLenth = inputStream.available();
int bufferSize = inputStreamBytesLenth / BUFFER_SIZE;
int endBytesLength = inputStreamBytesLenth - bufferSize * BUFFER_SIZE;
for(int i = 0;i < bufferSize;i++){
// , 0,
inputStream.read(bytes);
totalBytes = byteMerge(totalBytes, bytes);
}
if(endBytesLength > 0){
inputStream.read(bytes);
bytes = subBytes(bytes, 0, endBytesLength);
totalBytes = byteMerge(totalBytes, bytes);
}
return new String(totalBytes);
}
private static byte[] subBytes(byte[] b,int off,int length){
byte[] b1 = new byte[length];
System.arraycopy(b, off, b1, 0, length);
return b1;
}
private static byte[] byteMerge(byte[] byte_1, byte[] byte_2){
byte[] byte_3 = new byte[byte_1.length+byte_2.length];
// System.arraycopy( , , , , )
// System.arraycopy , , ,
System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
return byte_3;
}
}