📄 tcpserver.java
字号:
/**
* This class models a server using TCP to serve the client.
*
* @author tyrant
* @version 1.0.0
*/
import java.net.*;
import java.io.*;
public class TCPServer {
/* server socket to serve the request from the client. */
private ServerSocket serverSocket;
/**
* constructor to create the socket.
* @throws IOException
*/
public TCPServer() throws IOException {
serverSocket = new ServerSocket(6000);
System.out.println("Server starts...");
}
/**
* method to exchange one byte with the server for 1000 times.
*/
public void run() {
// byte arrays to store the bytes to receive or to send
byte[] receiveByte = new byte[1];
byte[] sendByte = new byte[1];
try {
/*
* solve the request from the client all the time.
*/
while (true) {
// a socket to connect the client.
Socket connectSocket = serverSocket.accept();
// iostreams to communicate with the client.
DataOutputStream outToClient = new DataOutputStream(connectSocket
.getOutputStream());
DataInputStream inFromClient = new DataInputStream(connectSocket
.getInputStream());
// exchange one byte with the client.
inFromClient.read(receiveByte, 0, receiveByte.length);
outToClient.write(sendByte, 0, sendByte.length);
connectSocket.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
TCPServer server = new TCPServer();
server.run();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -