- import java.io.*;
- import java.net.*;
- class
- {
- public static void main(String[] args)
- {
- Socket s = new Socket("192.168.1.254",10004);
- OutputStream out = s.getOutputStream();
- out.write("服务端,你好".getBytes());
- InputStream in = s.getOutputStream();
- byte[] buf = new byte[1024];
- int len = in.read(buf);
- System.out.println(new String(buf,0,len));
- s.close();
- }
- }
- class TcpServer2
- {
- 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"....connected");
- 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());
- }
- }
复制代码
为什么可以有两个main呢?而且编译的时候还可以先编译.java文件在分别编译两个类啊? |