import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
//客户端
public class SocketDome {
public static void main(String[] args) throws IOException {
Socket s = new Socket("192.168.22.36",32345);
OutputStream os = s.getOutputStream();
os.write("are you 大哥?".getBytes());
InputStream is = s.getInputStream();
byte [] by = new byte[1024];
int len = is.read(by);
String st = new String(by,0,len);
System.out.println("客户端:"+st);
s.close();
os.close();
is.close();
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务器
public class ServerDome {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(32345);
Socket s = ss.accept();
InputStream is = s.getInputStream();
byte [] by = new byte[1024];
int len = is.read(by);
String s1 = new String(by, 0, len);
System.out.println(s1);
OutputStream os = s.getOutputStream();
os.write("收到".getBytes());
ss.close();
s.close();
os.close();
}
}
目前学到网络编程来了,放一个国庆长假后发现之前学的,类,方法全忘了。学的脑壳疼 |
|