聊天程序服务器 class 不发送消息
Chat program server class does not send messages
我有这个来自客户端-服务器聊天程序的服务器代码,但我想当我 运行 它我每次只能发送一个词,例如 "sending hello to 1" 没问题,它发送 'hello' 给客户 1,但是如果我想写一个句子,我会得到 "Invalid Message"。我已经尝试修复它一段时间了,但我找不到正确的方法来获取消息以及消息应该发送到的客户端数量。
public class Serverr implements Runnable {//server implements threading by implementing runnable interface
Socket csocket;
static HashMap<Integer,Socket>hm=new HashMap <Integer,Socket>(); //here we store each client socket in hashmap
static int k=1;
Serverr(Socket csocket){
this.csocket=csocket;//Server ser=new Server(sock); comes here and assigns it to csocket
}
public static void main(String args[])
throws Exception{
ServerSocket ssock=new ServerSocket(5000);//Socket on port 5000(same as mentioned inclient class)
System.out.println("Listening");//when connected on port 5000 we print listening
while(true){
Socket sock=ssock.accept();//accept client socket
Serverr ser=new Serverr(sock);//pass socket to constructor of server class
new Thread(ser).start();//start thread here
hm.put(k,sock);//add the socket into hashmap
System.out.println("Connected to client"+k);//print the number of connected client
k++;
}
}
@Override
public void run() {
try{
//once the thread is started we initialize necessary input & output streams here
PrintWriter out;
BufferedReader in=new BufferedReader(new InputStreamReader(csocket.getInputStream()));//used to read input from the socket
String inputLine;
String j="sending"; //follow a pattern for sending messages
String l="to";
String t="sendtoall";//used to sent message to all clients
while((inputLine=in.readLine())!=null)//if the message from connected client !=null
{
String a[]=inputLine.split(" ");//split the input line using space if a[0] is sendmsg and a[2] is to
if(a[0].equals(j) && a[2].equals(l))//we started the server
{
int id=Integer.parseInt(a[3]);//we will get the number of client here
if(hm.containsKey(id)){// we will check hashmap if it contains id
Socket ser1=hm.get(id);//here we will get that clients socket from hashmap
out=new PrintWriter(ser1.getOutputStream(),true);// we will write to that socket using output stream
out.println(a[1]);//we will get that specific message part
out.flush();// a[1] is...
}
else
{
out=new PrintWriter(csocket.getOutputStream(),true);
out.println("user offline");//we print it if hashmap doesnt contain the key value
out.flush();
}
}
else if(a[0].equals(t))//if we want to sent message to all clients at once
{
for(int h=1;h<hm.size();h++)//loop through hashmap & get those socket value
{
Socket ser1=hm.get(h);
out=new PrintWriter(ser1.getOutputStream(),true);//we will write to that socket here using outputstream
out.println(a[1]);
out.flush();
}
}
else{
out=new PrintWriter(csocket.getOutputStream(),true);
out.println("Invalid Message");//if the message format doesnt match
out.flush();
}
}
}catch(IOException e){
System.out.println(e);
}
}
}
你的问题是这两行。
if(a[0].equals(j) && a[2].equals(l))//we started the server
{
int id=Integer.parseInt(a[3]);//we will get the number of client here
您正在检查 "to" 输入的第三个和第四个字以及客户编号。但是如果你有一个完整的句子,那么这些就不会是输入的第三个和第四个词。相反,您可能想分别检查 second-to-last 和最后的话,就像这样。
if(a[0].equals(j) && a[a.length - 2].equals(l))//we started the server
{
int id=Integer.parseInt(a[a.length - 1]);//we will get the number of client here
这将允许您输入
sending Hello my friend to 12345
并正确识别字段。
您还必须更改线路
out.println(a[1]);
这样它发送的不仅仅是一个单词。也许您可以使用循环来执行此操作,例如这样。
for (int word = 1; word < a.length - 2; word++ ) {
out.print(a[word]);
}
out.println();
我有这个来自客户端-服务器聊天程序的服务器代码,但我想当我 运行 它我每次只能发送一个词,例如 "sending hello to 1" 没问题,它发送 'hello' 给客户 1,但是如果我想写一个句子,我会得到 "Invalid Message"。我已经尝试修复它一段时间了,但我找不到正确的方法来获取消息以及消息应该发送到的客户端数量。
public class Serverr implements Runnable {//server implements threading by implementing runnable interface
Socket csocket;
static HashMap<Integer,Socket>hm=new HashMap <Integer,Socket>(); //here we store each client socket in hashmap
static int k=1;
Serverr(Socket csocket){
this.csocket=csocket;//Server ser=new Server(sock); comes here and assigns it to csocket
}
public static void main(String args[])
throws Exception{
ServerSocket ssock=new ServerSocket(5000);//Socket on port 5000(same as mentioned inclient class)
System.out.println("Listening");//when connected on port 5000 we print listening
while(true){
Socket sock=ssock.accept();//accept client socket
Serverr ser=new Serverr(sock);//pass socket to constructor of server class
new Thread(ser).start();//start thread here
hm.put(k,sock);//add the socket into hashmap
System.out.println("Connected to client"+k);//print the number of connected client
k++;
}
}
@Override
public void run() {
try{
//once the thread is started we initialize necessary input & output streams here
PrintWriter out;
BufferedReader in=new BufferedReader(new InputStreamReader(csocket.getInputStream()));//used to read input from the socket
String inputLine;
String j="sending"; //follow a pattern for sending messages
String l="to";
String t="sendtoall";//used to sent message to all clients
while((inputLine=in.readLine())!=null)//if the message from connected client !=null
{
String a[]=inputLine.split(" ");//split the input line using space if a[0] is sendmsg and a[2] is to
if(a[0].equals(j) && a[2].equals(l))//we started the server
{
int id=Integer.parseInt(a[3]);//we will get the number of client here
if(hm.containsKey(id)){// we will check hashmap if it contains id
Socket ser1=hm.get(id);//here we will get that clients socket from hashmap
out=new PrintWriter(ser1.getOutputStream(),true);// we will write to that socket using output stream
out.println(a[1]);//we will get that specific message part
out.flush();// a[1] is...
}
else
{
out=new PrintWriter(csocket.getOutputStream(),true);
out.println("user offline");//we print it if hashmap doesnt contain the key value
out.flush();
}
}
else if(a[0].equals(t))//if we want to sent message to all clients at once
{
for(int h=1;h<hm.size();h++)//loop through hashmap & get those socket value
{
Socket ser1=hm.get(h);
out=new PrintWriter(ser1.getOutputStream(),true);//we will write to that socket here using outputstream
out.println(a[1]);
out.flush();
}
}
else{
out=new PrintWriter(csocket.getOutputStream(),true);
out.println("Invalid Message");//if the message format doesnt match
out.flush();
}
}
}catch(IOException e){
System.out.println(e);
}
}
}
你的问题是这两行。
if(a[0].equals(j) && a[2].equals(l))//we started the server
{
int id=Integer.parseInt(a[3]);//we will get the number of client here
您正在检查 "to" 输入的第三个和第四个字以及客户编号。但是如果你有一个完整的句子,那么这些就不会是输入的第三个和第四个词。相反,您可能想分别检查 second-to-last 和最后的话,就像这样。
if(a[0].equals(j) && a[a.length - 2].equals(l))//we started the server
{
int id=Integer.parseInt(a[a.length - 1]);//we will get the number of client here
这将允许您输入
sending Hello my friend to 12345
并正确识别字段。
您还必须更改线路
out.println(a[1]);
这样它发送的不仅仅是一个单词。也许您可以使用循环来执行此操作,例如这样。
for (int word = 1; word < a.length - 2; word++ ) {
out.print(a[word]);
}
out.println();