使用Socket、SeverSocket,利用线程实现的简单的一对一聊天

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CrazyCoding123/article/details/51812738

这是本人第一篇博客,Android学习已经开始一个多月了,目前还处于JAVA阶段,最近学习到了TCP/IP协议在JAVA中得实现,利用封装好的Socket、SeverSocket类实现了简单的一对一聊天 。

最开始只是利用while循环实现了一人一句的聊天,结合之前学习到的线程,把发送消息和接受消息放在不同的线程中,实现了比较自由的来聊天。

代码如下:

package talk_OneToOne;
/**

客户端代码

*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Talk_Client {
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
Socket socket=new Socket("127.0.0.1", 8989);
//创建接收线程对象
Talk_Receive_C tr=new Talk_Receive_C(socket);
tr.start();//启动线程
//创建发送线程对象
Talk_Send_C ts=new Talk_Send_C(socket);
ts.start();//启动线程
}

//发送消息
private static class Talk_Send_C extends Thread{

private Socket socket;
public Talk_Send_C(Socket s) {
// TODO Auto-generated constructor stub
socket =s;
}
@Override
public void run() {
Scanner scanner =new Scanner(System.in);
try {
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
while (true) {
bw.write(scanner.nextLine());
bw.newLine();
bw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**客户端一直接收消息*/
private static class Talk_Receive_C extends Thread{


private Socket socket;
public Talk_Receive_C(Socket s) {
// TODO Auto-generated constructor stub
socket=s;
}
@Override
public void run() {
try {
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
System.out.println("服务器说:"+br.readLine());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


package talk_OneToOne;
/**

服务端代码

*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Talk_Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=new ServerSocket(8989);
System.out.println("服务器已经启动……");
Socket s=serverSocket.accept();
new Talk_Receive(s).start();//创建接收线程并启动
new Talk_Send(s).start();//创建发送线程并启动
}
//静态内部类继承线程类:发送信息
private static class Talk_Send extends Thread
{
private Socket socket;
public Talk_Send(Socket s) {
// TODO Auto-generated constructor stub
socket=s;
}
@Override
public void run() {
Scanner scanner=new Scanner(System.in);
try {
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
while (true) {
bw.write(scanner.nextLine());
bw.newLine();
bw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
//接受信息
private static class Talk_Receive extends Thread
{
private Socket socket;
public Talk_Receive(Socket s) {
// TODO Auto-generated constructor stub
socket=s;
}
@Override
public void run() {
try {
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
System.out.println("客户端说:"+br.readLine());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

本代码还没有实现聊天的退出,只能通过非正常手段。

本人还是小白,欢迎大家前来指正、交流、学习

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页