A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

class TcpClient
{   public static void main (String [] args) throws Exception
     { Socket s = new Socket("192.168.1.255",10004);
       OutputStream out= s.getOutStream();
       out.write("服务端你好,我来了");
       InputSteram in = new InputStream();
       byte[] buf = new byte[1024];
       int len = in.read(buf);
       System.out.println(new String(buf,0,len));
        s.close();
      
     }
}



class TcoServert
{    public static void main (String [] args) throws Exception
     {   ServerSocket ss= new ServerSocket(10004);
         Socket s= ss.accept();
         String ip = s.getInetaddress().getHostAddress();
         System.out.println(ip+"...connection");
         InputStream in = s.getInputStream();
          byte[] buf = new byte[1024];
           int len = in.read(buf);
        System.out.println(new String(buf,0,len));
        OutputStram out = s.getOutputStream();
        out.write("我收到服务器的回答了".getBytes());
         s.close();
         ss.close();
     }
}

1 个回复

倒序浏览
贴出来验证完的import java.io.*;
import java.net.*;
class TcpClient
{   public static void main (String [] args) throws Exception
     { Socket s = new Socket("192.168.1.255",10004);
       OutputStream out= s.getOutputStream();
       out.write("服务端你好,我来了".getBytes());
       InputStream in = s.getInputStream();
       byte[] buf = new byte[1024];
       int len = in.read(buf);
       System.out.println(new String(buf,0,len));
        s.close();
      
     }
}



class TcoServert
{    public static void main (String [] args) throws Exception
     {   ServerSocket ss= new ServerSocket(10004);
         Socket s= ss.accept();
         String ip = s.getInetAddress().getHostAddress();
         System.out.println(ip+"...connection");
         InputStream in = s.getInputStream();
          byte[] buf = new byte[1024];
           int len = in.read(buf);
        System.out.println(new String(buf,0,len));
        OutputStream out = s.getOutputStream();
        out.write("我收到服务器的回答了".getBytes());
         s.close();
         ss.close();
     }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马