服务器端源代码
import java.io.*;
import java.lang.String;
import java.net.ServerSocket;
import java.net.Socket;
public class serve 
{
private ServerSocket lis;//监听socket
private Socket acc[];     //会话socket声明为数组,以为每一个会话建立一个tcp连接
private String rev;
private InputStream in[];
private byte count=5;         //记录会话socket的个数
  private byte b[];
public serve()
{
serveInit();
rec();
}
public void serveInit()
{
try
{
 lis=new ServerSocket(10015,5);
 lis.setSoTimeout(1000);
 System.out.println(lis.getLocalSocketAddress());
 b=new byte[1024];
 acc=new Socket[5];
 in=new InputStream[5];
}
catch(IOException ie)
{
System.out.println("I/O error!");
}
catch(Exception e)
{
  e.printStackTrace();
}
}
public void rec()

while(true)
{
try
{
 for(byte i=0;i<count;i++)
 {  
    if(acc[i].isConnected()==false)
    {
     acc[i]=lis.accept();
     System.out.println("启动监听程序!");
     if(acc[i].isBound()==true)
     { 
     in[i]=acc[i].getInputStream();
       rev=acc[i].getInetAddress().getHostName();
      System.out.println(rev+":"+acc[i].getPort()+"建立连接");
    }
     break;
    }
 }
 for(byte i=0;i<count;i++)
 {
  if(acc[i].isConnected()==true)
  {
   int len=in[i].read(b);
   while(len!=-1)
   {
     String temp=new String(b,0,len);
     rev+=temp;
     System.out.println(rev);
     len=in[i].read(b);
   }
  }
  }
}
catch(IOException ro)
{
ro.toString();
}
catch(Exception e)
{
e.toString();
}
}
 }
public static void main(String args[])
{
new serve();
}
}
客户端源代码
import java.io.*;
import java.net.Socket;
import java.lang.*;
public class client
{
private Socket con;//客户端连接socket
private OutputStream out;
private String sen;
private byte b[];
public client()
{
clientInit();
}
public void clientInit()
{
try
{
 con=new Socket("219.245.126.96",10015);
 con.setSoTimeout(2000);
 b=new byte[1024];
 OutputStream out=con.getOutputStream();
 sen="hello serve";
 b=sen.getBytes();
 out.write(b);
 out.flush();
}
catch(IOException ie)
{
ie.toString();
}
}
public static void main(String args[])
{
new client();
}
}