import java.io.*;
import java.net.*;
import java.util.*;
public class fuwuqi
{
public static void main(String[] args)
{
int count=0;
try
{
ServerSocket server=new ServerSocket(9876);
System.out.println("服务器对9876端口正在进行监听");
while(true)
{
Socket sc=server.accept();
DataInputStream din=new DataInputStream(sc.getInputStream());
DataOutputStream dout=new DataOutputStream(sc.getOutputStream());
System.out.println("欢迎你的访问");
dout.writeUTF("你好");
System.out.println(din.readUTF());
din.close();
dout.close();
sc.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
客户端
import java.io.*;
import java.net.*;
public class kehuduan
{
public static void main(String[] args)
{
try
{
Socket sc=new Socket("192.168.1.35",9876);
DataInputStream din=new DataInputStream(sc.getInputStream());
DataOutputStream dout=new DataOutputStream(sc.getOutputStream());
dout.writeUTF("你好");
System.out.println(din.readUTF());
din.close();
dout.close();
sc.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
我用netstat命令查看过,客户端开启的时候,端口没有被占用,处于监听模式,但是客户端就是无法发送数据给服务器
|
|