要完成一个java服务器,接收一个客户端的“hello”,然后发送到另一个客户端的功能

现在已经拼凑了一段服务器的代码,能做到接收一个客户端的“hello”,但是转发的那部分不会写,求高手帮忙修改这段代码,增加这个功能~我是新手以下是那段服务器的代码:pub... 现在已经拼凑了一段服务器的代码,能做到接收一个客户端的“hello”,但是转发的那部分不会写,求高手帮忙修改这段代码,增加这个功能~我是新手
以下是那段服务器的代码:

public class Server {
private int port=3333;
private ServerSocket serverSocket;
public Server() throws IOException {
serverSocket = new ServerSocket(port);
System.out.println("服务器启动");
}
public void service() {
while (true) {
Socket socket=null;
try {
socket = serverSocket.accept();
Thread serverThread=new Thread(new Handler(socket));
serverThread.start();
}catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[])throws IOException {
new Server().service();
}
}
class Handler implements Runnable{
private Socket socket;
public Handler(Socket socket){
this.socket=socket;
}

private BufferedReader br;
PrintWriter out;

public void run(){
try {
System.out.println("New connection accepted " +
socket.getInetAddress() + ":" +socket.getPort());
br =new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);

String msg = null;
while ((msg = br.readLine()) != null) {
System.out.println(socket+"you input is:"+msg);
out.println("get word");
if ("bye".equalsIgnoreCase(msg))
break;

}

}catch (IOException e) {
e.printStackTrace();
}finally {
try{
if(socket!=null)socket.close();
System.out.println("close");
}catch (IOException e) {e.printStackTrace();}
}
}
}
展开
 我来答
a11e90
2012-05-04 · 知道合伙人软件行家
a11e90
知道合伙人软件行家
采纳数:473 获赞数:1702
qq交流群:208779755 博客:https://my.oschina.net/haitaohu

向TA提问 私信TA
展开全部
package TalkHouse.TCP;

import java.net.*;
import java.io.*;

public class TalkServer {
public static void main(String[] args) {
int count = 0;
ServerSocket server = null;
try {
server = new ServerSocket(8888);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("server's port:" + server.getLocalPort());
while (true) {
Socket temp = new Socket();
try {
temp = server.accept();
} catch (IOException e) {
e.printStackTrace();
}
new TalkServerThread(temp, (count + 1)).start();
System.out.println("=====================");
count++;
System.out.println("Clinet " + count + " have connected");
}
}
public static void Aclose(Object...o){
for(int i=0;i<o.length;i++){
if(o[i] instanceof Closeable){
try {
Closeable c = (Closeable)o[i];
c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(o[i] instanceof ServerSocket){
try {
ServerSocket c = (ServerSocket)o[i];
c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(o[i] instanceof Socket){
try {
Socket c = (Socket)o[i];
c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/****************************************************************************************************/
package TalkHouse.TCP;

import java.io.*;
import java.net.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class TalkServerThread extends Thread {
Socket serverworker;
int workerID;
static Set<BufferedWriter>sockets = new HashSet<BufferedWriter>();
public TalkServerThread(Socket s, int c) {
this.serverworker = s;
this.workerID = c;
}
public void run() {
BufferedReader br = null;
BufferedWriter bw = null;
try {
String str = null;
br = new BufferedReader(new InputStreamReader(
serverworker.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(
serverworker.getOutputStream()));
sockets.add(bw);

while (true) {
str = br.readLine();
if(str.equals("end")){
System.out.println("Client " + workerID + "已经离开");
break;
}
System.out.print("Client " + workerID + " says: ");
System.out.println(str);
Iterator<BufferedWriter> it = sockets.iterator();
str = workerID + " says: "+str;
while(it.hasNext()){
BufferedWriter temp = it.next();
if(temp != bw){
temp.write(str+"\n");
temp.flush();
}
}
if (str.equals("end")) {
System.out.println("Client " + workerID + " OUT");
break;
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}finally{
Aclose(serverworker,br,bw);
sockets.remove(bw);
}
}
public static void Aclose(Object...o){
for(int i=0;i<o.length;i++){
if(o[i] instanceof Closeable){
try {
Closeable c = (Closeable)o[i];
c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(o[i] instanceof ServerSocket){
try {
ServerSocket c = (ServerSocket)o[i];
c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(o[i] instanceof Socket){
try {
Socket c = (Socket)o[i];
c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

以前自己写的简单聊天室的服务器部分 你参考下吧 不是很好但能完成转发
百度网友39b8b51
2012-05-04 · TA获得超过281个赞
知道小有建树答主
回答量:199
采纳率:0%
帮助的人:211万
展开全部
import java.net.*;
import java.util.LinkedList;
import java.io.*;

public class Server {
private int port=3333;
private ServerSocket serverSocket;
//创建一个集合存放所有的客户端
private LinkedList<Handler>clients=new LinkedList<Handler>();

public Server() throws IOException {
serverSocket = new ServerSocket(port);
System.out.println("服务器启动");
}
public void service() {
while (true) {
Socket socket=null;
try {
socket = serverSocket.accept();
//每连接一个客户端 就往集合里面放
Handler hd=new Handler(socket);
clients.add(hd);

Thread serverThread=new Thread(hd);
serverThread.start();
}catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[])throws IOException {
new Server().service();
}
private class Handler implements Runnable{
private Socket socket;
private BufferedReader br;
private PrintWriter out;

public Handler(Socket socket){
this.socket=socket;
}
//负责转发数据的方法
public void send(String msg){
try{
out = new PrintWriter(socket.getOutputStream(), true);
out.println(msg);
}catch(IOException ie){

ie.printStackTrace();
}
}

public void run(){
try {
System.out.println("New connection accepted " +
socket.getInetAddress() + ":" +socket.getPort());
br =new BufferedReader(new InputStreamReader(socket.getInputStream()));

String msg = null;
while ((msg = br.readLine()) != null) {
//服务器遍历集合里面的每个客户端,并发送数据
for(int i=0;i<clients.size();i++){
Handler hd=clients.get(i);
hd.send(msg);
}

System.out.println(socket+"you input is:"+msg);
//out.println("get word");
if ("bye".equalsIgnoreCase(msg))
break;

}

}catch (IOException e) {

//e.printStackTrace();
}finally {
try{
clients.remove(this); //客户端离开时要移出集合
if(br!=null)br.close();
if(out!=null)out.close();
if(socket!=null)socket.close();
System.out.println("close");
}catch (IOException e) {

e.printStackTrace();
}
}
}
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式