import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Main {
public static void main(String[] args) throws IOException {
Socket s = new Socket(InetAddress.getByName("SKY-20180725WBH"), 10077);
OutputStream os = s.getOutputStream();
String str = "hello tcp, i am coming.";
os.write(str.getBytes());
os.close();
s.close();
}
}
服务端代码
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class Receive {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10077);
Socket cs = ss.accept();
System.out.println(cs);
InputStream is = cs.getInputStream();
byte[] bys = new byte[1024];
int len;
len = is.read(bys);
InetAddress address = ss.getInetAddress();
System.out.println(new String(bys, 0, len));
System.out.println(cs.getPort());
System.out.println(cs.getInetAddress());
System.out.println();
System.out.println(cs.getPort());
cs.close();
// ss.close(); //服务端可以不关
}
}