java socket 多线程 服务器端_使用Socket+多线程实现客户端和服务器端可以多发多收而不用等...

本文档展示了一个Java Socket编程的例子,客户端和服务器端都无法正常通信。服务器端一直在等待客户端连接,但无法接收到客户端的启动信号。问题可能在于线程处理或网络配置。代码中包含了Server和Client类,以及Send和Receive线程类的实现,用于发送和接收消息。请检查网络连接、端口是否开放以及线程同步问题。
摘要由CSDN通过智能技术生成

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

运行时,服务器端一直接收不到客户端已经启动,一直在等待中,我写了两个线程类,一个用于发消息,一个用于收消息,ip和端口号均正确,求大佬指教问题在哪,代码如下

Server

package cn.com.socket;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import java.net.SocketException;

public class Server{

private ServerSocket server;

public static void main(String[] args) {

new Server();

}

public Server() {

try {

server = new ServerSocket(9001);

System.out.println("等待客户端连接...");

chat();

} catch (SocketException se) {

se.printStackTrace();

} catch (IOException e1) {

e1.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

private void chat() {

try {

new Send(server.accept(),"服务器").start();

new Receive(server).start();

} catch (IOException e) {

e.printStackTrace();

}

}

}

Client

package cn.com.socket;

import java.io.IOException;

import java.net.Socket;

import java.net.SocketException;

public class Client{

private Socket server;

public static void main(String[] args) {

new Client();

}

public Client() {

try {

server = new Socket("192.168.43.235", 9001);

chat();

} catch (SocketException se) {

se.printStackTrace();

} catch (IOException e1) {

e1.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

private void chat() {

new Send(server,"客户端").start();

new Receive(server).start();

}

}

以下是那两个线程:

Send

package cn.com.socket;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.Socket;

public class Send extends Thread{

private Socket s;

private String name;

private PrintWriter out;

private BufferedReader br;

public void run() {

String msg;

try {

msg = this.name+" : "+"你好";

out.println(msg);

while(!msg.endsWith("88")) {

msg = br.readLine();

out.println(this.name+" :"+msg);

}

System.out.println("聊天结束!");

} catch (IOException e) {

e.printStackTrace();

} finally {

if(out != null) {

out.close();

}

if(s != null) {

try {

s.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public Send(Socket socket,String name) {

this.name = name;

this.s = socket;

this.br = new BufferedReader(new InputStreamReader(System.in));

try {

this.out = new PrintWriter(s.getOutputStream(),true);

} catch (IOException e) {

e.printStackTrace();

}

}

}

Receive

package cn.com.socket;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

public class Receive extends Thread{

private Socket s;

private ServerSocket ss;

private PrintWriter out;

private BufferedReader in;

@Override

public void run() {

String acc;

try {

acc = in.readLine();

System.out.println(acc);

while (!acc.endsWith("88")) {

acc = in.readLine();

out.println(acc);

System.out.println(acc);

}

System.out.println("聊天结束!");

} catch (IOException e) {

e.printStackTrace();

} finally {

if(in != null) {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if(out != null) {

out.close();

}

if(this.s != null) {

try {

this.s.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if(this.ss != null) {

try {

this.ss.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public Receive(Socket socket) {

this.s = socket;

try {

this.out = new PrintWriter(s.getOutputStream());

this.in = new BufferedReader(new InputStreamReader(s.getInputStream()));

} catch (IOException e) {

e.printStackTrace();

}

}

public Receive(ServerSocket ssocket) throws IOException {

this(ssocket.accept());

this.ss = ssocket;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值