📄 tcpserver.java
字号:
import java.io.*;
import java.net.*;
/*
* Created on 2008-4-12
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author Guest
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TCPServer {
/*
* Define the size of buffer to make sure that one packet contain one byte.
*/
private static int bufferSize = 1;
/*
* Define the default port for the packet send to.
*/
private static int defaultPort = 8000;
/*
* The main(String[]) method.
*/
public static void main(String[] args)throws Exception {
/*
* The out to client Stream.
*/
DataOutputStream outToClient;
/*
* The input from client stream.
*/
DataInputStream inFromClient;
/*
* Create a tcp server socket.
*/
ServerSocket welcomeSocket = new ServerSocket(defaultPort);
/*
* Declarate the receive byte.
*/
byte receiveBuffer[] = new byte[bufferSize];
System.out.println("connecting...");
/*
* Initial the receive byte.
*/
receiveBuffer[0] = 0;
while(true)
{
try
{
Socket connectionSocket = welcomeSocket.accept();
outToClient = new DataOutputStream(connectionSocket.getOutputStream());
inFromClient = new DataInputStream(connectionSocket.getInputStream());
inFromClient.read(receiveBuffer);
outToClient.write(receiveBuffer);
outToClient.flush();
}catch (Exception e) {
e.printStackTrace();
System.err.println("Error!");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -