package tcp;import java.io.*;
import java.net.*;
public class ServerCode
{
public static int portNO=3333;
public static void main(String[] args)throws IOException
{
ServerSocket s=new ServerSocket(portNO);
System.out.println("服务器启动:"+s);
Socket socket=s.accept();
System.out.println("有客户端请求");

try
{
System.out.println("接受客户端请求:"+socket);
InputStream is=socket.getInputStream();
OutputStream os=socket.getOutputStream();

os.write("你好,客户端".getBytes());
//----------------------------------------------------------------------
byte[] b=new byte[100];
int len=is.read(b);
System.out.println(new String(b,0,len));
//----------------------------------------------------------------------


}
finally
{
System.out.println("close th Server cocket and the io");

}
}
}
package tcp;import java.io.*;
import java.net.*;public class ClientCode
{
public static String clientName="Mike";
public static int portNO=3333;
public static void main(String[] args)throws IOException
{
//设置本地连接
InetAddress addr=InetAddress.getByName("localhost");
Socket socket=new Socket(addr,portNO);
try
{
System.out.println("socket="+socket);
InputStream is=socket.getInputStream();
OutputStream os=socket.getOutputStream();

byte[] b=new byte[100];
int len=is.read(b);
System.out.println(new String(b,0,len));
//----------------------------------------------------------------------
os.write("你好,服务端!".getBytes());

}
finally
{
System.out.println("close the Client socket and the io.");

}
}}这个程序运行完成后的结果是:
//---------------------------------------------------------------------
服务器启动:ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=3333]
有客户端请求
接受客户端请求:Socket[addr=/127.0.0.1,port=1144,localport=3333]
你好,服务端!
close th Server cocket and the io
//---------------------------------------------------------------------为什么服务端发的
os.write("你好,客户端".getBytes());客户端收不到啊,是不是程序那里有问题啊?
我用的 Eclipse 调试的!
大家帮我看看吧!我弄了N次都不行
我试过 服务器端发送1次,客户端接收 可以
       客户端发送1次,服务器端接收 可以
       但是如果服务器端发送1次接收1次,而客户端也发送1次接收1次就会出问题。总是有一个会接不到消息