java多线程socket实现简单的聊天

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();
}

}
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值