server端:需要注意的是死循环acept,然后对每一个客户端开两个线程,分别是读写,这样使得可以全双工通信,另外注意读写的方法,比如读的时候的nextLine,要记得在发送的时候加上换行符,否则读不到。
package com.min.testsocket;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server {
private static class Send implements Runnable{
private Socket socket;
public Send(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
PrintWriter pWriter=null;
Scanner scanner=null;
try {
pWriter=new PrintWriter(socket.getOutputStream());
scanner=new Scanner(System.in);
while (true) {
String s=scanner.nextLine();
pWriter.write(s+"\n");
pWriter.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
scanner.close();
pWriter.close();
}
}
}
private static class Receive implements Runnable{
private Socket socket;
public Receive(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
Scanner scanner=null;
try {
scanner=new Scanner(socket.getInputStream());
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
// while (true) {
// String s=scanner.next();
// System.out.println(s);
// }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
scanner.close();
}
}
}
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=new ServerSocket(8888);
while (true) {
Socket socket=serverSocket.accept();
new Thread(new Send(socket)).start();
new Thread(new Receive(socket)).start();
}
}
}
client端:与服务端类似,开两个线程分别读写
package com.min.testsocket;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
private static class Receive implements Runnable {
private Socket socket;
public Receive(Socket socket) {
this.socket=socket;
}
public void run() {
Scanner scanner=null;
try {
scanner=new Scanner(socket.getInputStream());
// while (scanner.hasNext()) {
// System.out.println(scanner.next());
// }
while (true) {
String s=scanner.nextLine();
System.out.println(s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
scanner.close();
}
}
}
private static class Send implements Runnable{
private Socket socket;
public Send(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
PrintWriter pWriter=null;
Scanner scanner=null;
try {
pWriter=new PrintWriter(socket.getOutputStream());
scanner=new Scanner(System.in);
while (true) {
String s=scanner.nextLine();
pWriter.write(s+"\n");
pWriter.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
scanner.close();
pWriter.close();
}
}
}
public static void main(String[] args) {
try {
Socket socket=new Socket("118.89.22.152", 8888);
new Thread(new Send(socket)).start();
new Thread(new Receive(socket)).start();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}