- import java.net.*;
- import java.io.*;
- public class TCPDemo
- {
- public static void main(String[] args) throws Exception
- {
- // TODO 自动生成的方法存根
-
- Socket s = new Socket("192.168.1.103",11908);//这句代码有什么问题吗?
- ServerSocket ss = new ServerSocket(11908);
-
- new Thread(new TCPServerDemo(ss)).start();
- new Thread(new TCPClientDemo(s)).start();
- }
- }
- class TCPClientDemo implements Runnable
- {
- private Socket s;
- public TCPClientDemo(Socket s)
- {
- this.s = s;
- }
- public void run()
- {
- try{
- Thread.sleep(2000);
- OutputStream out = s.getOutputStream();
- out.write("TCP ge men lai le ".getBytes());
- s.close();
- }
- catch(Exception e)
- {
- throw new RuntimeException("客户端异常");
- }
- }
- }
- class TCPServerDemo implements Runnable
- {
- private ServerSocket ss;
- public TCPServerDemo(ServerSocket ss)
- {
- this.ss = ss;
- }
- public void run()
- {
- // TODO 自动生成的方法存根
- try{
- Socket s = ss.accept();
- InputStream in = s.getInputStream();
- byte[] buf = new byte[1024];
- int len = in.read(buf);
- String ip = s.getInetAddress().getHostAddress();
- System.out.println(ip+new String(buf,0,len));
- s.close();
- ss.close();
- }
- catch(Exception e)
- {
- throw new RuntimeException("服务端异常");
- }
- }
- }
复制代码
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at TCPDemo.main(TCPDemo.java:10)
求高手给解答下啊
|