[英]Can't send message to all Clients Java
I'm working with A Server which can receive message from all clients and send them to others.我正在使用一个服务器,它可以从所有客户端接收消息并将它们发送给其他人。 I can receive message but I cant sen to all clients although I get the correct out Stream from all clients.我可以接收消息,但我无法向所有客户端发送消息,尽管我从所有客户端获得了正确的输出流。
This is my code If you want to test, please cmd -> telnet localhost port number这是我的代码 如果要测试,请 cmd -> telnet localhost 端口号
import java.io.*;
import java.util.*;
import java.net.*;
public class Server
{
private ServerSocket s;
private Hashtable saveOutputStreams = new Hashtable(500,80);
//Constructor
public Server(int port) throws IOException
{
listen(port);
}
//Listen method
public void listen(int port) throws IOException
{
s = new ServerSocket(port);
while(true)
{
Socket incoming = s.accept();
OutputStream sout = incoming.getOutputStream();
saveOutputStreams.put(incoming, sout);
Runnable r = new ThreadHandler(this,incoming);
Thread t = new Thread(r);
t.start();
}
}
// Create a table of streams to process in for loop of senToAll method
Enumeration getOutputStreams()
{
return saveOutputStreams.elements();
}
// Send message to all clients
public void sendToAll(String message)
{
synchronized(saveOutputStreams)
{
for(Enumeration e = getOutputStreams();e.hasMoreElements();)
{
OutputStream getOut = (OutputStream)e.nextElement();
PrintWriter outp = new PrintWriter(getOut);
try
{
outp.println("sent"+ message);
System.out.println("Stream: "+getOut);
System.out.println("PrinWriter "+outp);
}
catch(Exception ie)
{
ie.printStackTrace();
System.out.println("Error");
}
finally
{
System.out.println("done sen To All");
}
}
}
}
// Main
public static void main(String []args) throws IOException
{
new Server(8015);
}
}
class ThreadHandler implements Runnable
{
private Socket incoming;
private Server serverP;
public ThreadHandler(Server server, Socket socket)
{
serverP = server;
incoming = socket;
}
public synchronized void run()
{
try
{
try
{
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();
Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream,true);
out.println("TungAnh'Server");
String message = in.nextLine();
serverP.sendToAll(message);
out.println("receieve: "+message);
out.println("done");
System.out.println("current Stream: "+ outStream);
System.out.println("PrinWriter "+ out);
}
finally
{
incoming.close();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
I improved your code a little bit:我稍微改进了你的代码:
If you are going to use my code, you should definitely have a look at synchronization (my methods are synchronized, but they should be synchronized with the same locking object/class) and exception handling.如果你打算使用我的代码,你绝对应该看看同步(我的方法是同步的,但它们应该与相同的锁定对象/类同步)和异常处理。 I could not spend more time with this issue, sorry for that.我不能在这个问题上花更多的时间,很抱歉。
Result (works for me):结果(对我有用):
package test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Server
{
private ServerSocket s;
//Constructor
public Server(int port) throws IOException
{
listen(port);
}
//Listen method
public void listen(int port) throws IOException
{
s = new ServerSocket(port);
while(true)
{
Socket incoming = s.accept();
Runnable r = new ThreadHandler(this,incoming);
Thread t = new Thread(r);
t.start();
}
}
// Main
public static void main(String []args) throws IOException
{
new Server(8015);
}
}
class ThreadHandler implements Runnable
{
private Socket incoming;
private static List<ThreadHandler> instances = new ArrayList<ThreadHandler>();
public synchronized void addInstance() {
instances.add(this);
}
public synchronized void removeInstance() {
instances.remove(this);
}
public ThreadHandler(Server server, Socket socket)
{
incoming = socket;
addInstance();
}
public synchronized void sendToAll(String msg, ThreadHandler me) {
for (ThreadHandler h : instances) {
if (h == me) continue;
OutputStream outStream = null;
PrintWriter print = null;
try {
outStream = h.incoming.getOutputStream();
print = new PrintWriter(outStream);
print.println(msg);
print.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public synchronized void run()
{
try
{
try
{
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();
Scanner in = null;
try {
in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream,true);
out.println("Vojta's Server");
while (true) {
try {
String message = in.nextLine();
sendToAll(message, this);
} catch (Exception e) {
}
}
} catch (Exception e) {
}
finally {
if (in != null) {
in.close();
}
}
}
finally
{
incoming.close();
}
}
catch(IOException e)
{
e.printStackTrace();
} finally {
removeInstance();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.