📄 appserver.java
字号:
import java.awt.event.*;
import java.io.*;
import java.net.*;
class Customer implements Serializable
{
String custName;
String custPassword;
}
public class AppServer extends Thread
{
ServerSocket serverSocket;
public AppServer()
{
try
{
serverSocket=new ServerSocket(8080);
}
catch(IOException e)
{
fail(e,"Could not start server.");
}
System.out.println("Server starting...");
this.start();
}
public static void fail(Exception e,String str)
{
System.out.println(str+" . "+e);
}
public void run()
{
try
{
while(true)
{
Socket client=serverSocket.accept();
Connection con=new Connection(client);
}
}
catch(IOException e)
{
fail(e,"Not listening");
}
}
public static void main(String args[])
{
new AppServer();
}
}
class Connection extends Thread
{
protected Socket netClient;
protected ObjectInputStream fromClient;
protected PrintStream toClient;
public Connection(Socket client)
{
netClient = client;
try
{
fromClient=new ObjectInputStream(netClient.getInputStream());
toClient=new PrintStream(netClient.getOutputStream());
}
catch(IOException e)
{
try
{
netClient.close();
}
catch(IOException e1)
{
System.out.println("Unable "
+" to set up stream"+e1);
return;
}
}
this.start();
}
public void run()
{
Customer clientMessage;
try
{
for(;;)
{
clientMessage=(Customer)fromClient.readObject();
if(clientMessage==null)
break;
toClient.println("Received" +" from:"+clientMessage.custName+":"+clientMessage.custPassword);
}
}
catch(IOException e)
{
}
catch(ClassNotFoundException e1)
{
System.out.println("Error in reading object "+e1);
}
finally
{
try
{
netClient.close();
}
catch(IOException e)
{}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -