单线程版本:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
//服务端
public class Server {
public static void main(String[] args) {
try {
//Socket套接字(TCP/IP)
//设置端口号
ServerSocket server = new ServerSocket(6666);
Socket s = server.accept();
PrintWriter out = new PrintWriter(s.getOutputStream());
BufferedReader br = new BufferedReader(new
InputStreamReader(s.getInputStream()));
System.out.println("已连接");
while (true) {
String sss = br.readLine();
System.out.println(sss);
System.out.print("请回复消息:");
out.write((new Scanner(System.in)).nextLine()+"\r\n");
out.flush();
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
//客户端
public class Client {
public static void main(String[] args) {
try {
//输入服务端的ip地址和端口号
//ip地址可以用控制台ipconfig命令查询
Socket s = new Socket("192.168.21.28", 6666);
BufferedReader br = new BufferedReader(new
InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream());
while (true) {
System.out.print("请输入:");
String sss = (new Scanner(System.in)).nextLine();
out.write(sss+"\r\n");
out.flush();
System.out.println(br.readLine());
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
A发完1条消息后,B回复1条后,A才能继续发送1条消息,B也同理
多线程版本:Server、Client、Read接收类、Send发送类
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Scanner;
/**
* Server.java 是服务端类,还有Read线程类,Send线程类
*/
public class Server {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(6688);
//accept()将会有一个中断等待
Socket a = server.accept();
System.out.println("有人连线");
new Thread(new Read(a)).start();
new Thread(new Send(a, "服务器")).start();
} catch (IOException e) {
System.out.println("服务器启动失败,端口6688被占用");
}
}
}
class Read implements Runnable {
Socket ss = null;
public Read(Socket ss) {
this.ss = ss;
}
@Override
public void run() {
BufferedReader br = null;
try {
br = new BufferedReader(new
InputStreamReader(ss.getInputStream()));
} catch (IOException e) {
throw new RuntimeException(e);
}
while (true) {
String sss = null;
try {
sss = br.readLine();
System.out.println(sss);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
class Send implements Runnable {
String name = "";
Socket ss = null;
public Send(Socket ss, String name) {
this.ss = ss;
this.name = name;
}
@Override
public void run() {
PrintWriter out = null;
try {
out = new PrintWriter(ss.getOutputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
while (true) {
out.printf("%s:%s\r\n", name, new Scanner(System.in).nextLine());
out.flush();
}
}
}
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
Socket c = new Socket("192.168.21.28", 6688);
new Thread(new Read(c)).start();
new Thread(new Send(c, "客户")).start();
} catch (Exception e) {
}
}
}
连接后可以随时发送和接收