📄 j_server.java
字号:
// ////////////////////////////////////////////////////////
//
// J_Server.java
//
// ////////////////////////////////////////////////////////
import java.net.*;
import java.io.*;
public class J_Server extends Thread
{
private Socket m_socket;
private int m_id; // Used to identify different thread.
public J_Server(Socket s, int id)
{
m_socket= s;
m_id= id;
} // End of the constructor
public void run( )
{
try
{
System.out.println("Socket["+m_id+"] connection success.");
PrintWriter out =new PrintWriter(new BufferedWriter(
new OutputStreamWriter(m_socket.getOutputStream( ))), true);
out.println("Socket["+m_id+"] welcome client.");
BufferedReader in=new BufferedReader(new InputStreamReader(m_socket.getInputStream( )));
while (true)
{
String ss= in.readLine( );
System.out.println("Socket["+m_id+"] receives: "+ss);
int n=ss.length( );
if (n>0)
{
char c= ss.charAt(n-1);
if (c=='q')
break;
}
} // End of the while loop
} // End of try
catch ( Exception e )
{ // Process EOFException & IOException
System.out.println( "Something wrong happens." );
}
finally
{
try
{
System.out.println("Socket["+m_id+"] is closing.");
m_socket.close( );
}
catch ( Exception e )
{
}
} // End of the structure try/catch/finally
} // End of method: run
public static void main(String args[])
{
int n= 1;
ServerSocket server= null;
try
{
server= new ServerSocket(8080);
System.out.println( "Server start." );
}
catch (IOException e)
{
}
while (true)
{
try
{
System.out.println("Wait NO. "+n+" connection." );
Socket s= server.accept( );
J_Server t=new J_Server(s, n++);
t.start( );
}
catch (IOException e)
{
}
} // End of loop: while
} // End of method: main
} // End of class: J_Server
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -