客户端
package Net.TCP.Demo6;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
class Client extends Thread{
Scanner sc=new Scanner(System.in);
@Override
public void run() {
while (true){
try (
Socket s = new Socket("127.0.0.1", 4443)) {
String s1 = sc.next();
OutputStream os = s.getOutputStream();
os.write(s1.getBytes());
// os.flush();
os.close();
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
-----------------------
服务端
-------------------
public class Serve {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(4444);
while (true){
Socket s = ss.accept();
new Client().start();
InputStreamReader is = new InputStreamReader(s.getInputStream());
int i;
while ((i=is.read())!=-1){
System.out.println((char)i);
}
System.out.println();
s.close();
}
}
}