?? myserver.java
字號(hào):
import java.io.*;
import java.net.*;
/****************************************************************
**
** 說(shuō) 明: Socket服務(wù)端<br>
**
****************************************************************/
class ServeOneJabber extends Thread
{
private Socket tSocket;
private BufferedReader in;
private PrintWriter out;
public ServeOneJabber(Socket s)
throws IOException
{
tSocket = s;
in = new BufferedReader(new InputStreamReader(tSocket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(tSocket.getOutputStream())), true);
start();
}
public void run()
{
try
{
while (true)
{
String str = in.readLine();
if(str.equals("END"))break;
System.out.println("Echoing: " + str);
out.println(str);
}
System.out.println("closing...");
}
catch (IOException e)
{
}
finally
{
try
{
tSocket.close();
}
catch(IOException e) {}
}
}
}
public class MyServer
{
static final int PORT = 8888;
public static void main(String[] args)
throws IOException
{
ServerSocket s = new ServerSocket(PORT);
System.out.println("Server Started");
try
{
while(true)
{
//接受客戶端連接請(qǐng)求
Socket socket = s.accept();
try
{
new ServeOneJabber(socket);
}
catch(IOException e)
{
socket.close();
}
}
}
finally
{
s.close();
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -