58,454
社区成员
发帖
与我相关
我的任务
分享
public class helloworld
{
public static void main(String[] args)
{
System.out.println("Hello World!!");
}
}
import java.net.*;
import java.io.*;
public class Javaserver
{
public static void main(String args[])
{
try
{
boolean flag=true;
Socket clientSocket=null;
String inputLine;
int c;
ServerSocket sSocket=new ServerSocket(8018);
System.out.println("Server listen on:"+sSocket.getLocalPort());
while(flag)
{
clientSocket=sSocket.accept();
DataInputStream is= new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
OutputStream os=clientSocket.getOutputStream();
while((inputLine=is.readLine())!=null)
{
if(inputLine.equals("stop"))
{
flag=false;
break;
}
else
{
System.out.println(inputLine);
while((c=System.in.read())!=-1)
{
os.write((byte)c);
if(c=='\n')
{
os.flush();
break;
}
}
}
}
is.close();
os.close();
clientSocket.close();
}
sSocket.close();
}
catch(Exception e)
{
System.out.println("Exception :"+ e.getMessage());
}
}
}