62,614
社区成员
发帖
与我相关
我的任务
分享
public class EchoServer
{
public static void main(String[] args)
{
try
{
ServerSocket s = new ServerSocket(8189);
Socket incoming = s.accept();
try
{
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();
Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream, true);
out.println("hello! enter BYE to exit.");
boolean done = false;
while(!done && in.hasNextLine())
{
String line = in.nextLine();
out.println("Echo:" + line);
if(line.trim().equals("BYE"))
done = true;
}
}
finally
{ incoming.close();}
}
catch(IOException e)
{
}
}
}