网络编程之tcp
程序员文章站
2024-02-01 13:52:28
...
- /**
- *TCPServer
- *@author Winty wintys@gmail.com
- *@version 2008-12-15
- */
- import java.io.*;
- import java.net.*;
- class TCPServer{
- public static void main(String[] args)throws IOException{
- ServerSocket listen = new ServerSocket(5050);
- Socket server = listen.accept();
- InputStream in = server.getInputStream();
- OutputStream out = server.getOutputStream();
- char c = (char)in.read();
- System.out.println("收到:" + c);
- out.write('s');
- out.close();
- in.close();
- server.close();
- listen.close();
- }
- }
- /**
- *TCPClient
- *@author Winty wintys@gmail.com
- *@version 2008-12-15
- */
- import java.io.*;
- import java.net.*;
- class TCPClient{
- public static void main(String[] args)throws IOException{
- Socket client = new Socket("127.0.0.1" , 5050);
- InputStream in = client.getInputStream();
- OutputStream out = client.getOutputStream();
- out.write('c');
- char c = (char)in.read();
- System.out.println("收到:" + c);
- out.close();
- in.close();
- client.close();
- }
- }