简体   繁体   English

Java客户端服务器聊天程序

[英]Java Client Server Chat Program

Guys am sick of this client and server chat program plz help me my program is compiled and runing but the problem is that when i trying to pass the msg to the server its not working it pass by itself..now what correction i do... 伙计们讨厌这个客户端和服务器聊天程序plz,可以帮助我编译和运行我的程序,但是问题是,当我尝试将msg传递到服务器时,它无法正常工作。 。

Server Code: 服务器代码:

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

class serv
{
     ServerSocket s;
     Socket c;
     DataInputStream dis;
     DataOutputStream dos;
     BufferedReader disi;

     public serv()
     {
          try
          {
               s = new ServerSocket(2000,0,InetAddress.getLocalHost());
               System.out.println("Server is Created");
               c = s.accept();
               System.out.println("Request Accepted");
          }
          catch(Exception e)
          {
               System.out.println(e);
          }
     }

     public void talk()throws IOException,UnknownHostException
     {
          dis = new DataInputStream(c.getInputStream());
          dos = new DataOutputStream(c.getOutputStream());
          disi = new BufferedReader(new InputStreamReader(System.in));

          while(true)
          {
               String str = new String(disi.readLine());
               dos.writeUTF(str);
               System.out.println(str);
          }
     }

     public static void main(String args[])
     {
          try
          {
               serv c = new serv();
               c.talk();
          }
          catch(Exception e)
          {
               e.printStackTrace();
          }
     }
}

Client Code: 客户代码:

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

class clien
{
     Socket c;
     DataInputStream dis;
     BufferedReader disi;
     DataOutputStream dos;

     public clien()throws IOException,UnknownHostException
     {
          c=new Socket(InetAddress.getLocalHost(),2000);
          System.out.println("Request is sended");
     }

     public void talk()throws IOException,UnknownHostException
     {
          try
          {
               dis=new DataInputStream(c.getInputStream());
               dos=new DataOutputStream(c.getOutputStream());
               disi=new BufferedReader(new InputStreamReader(System.in));

              while(true)
              {
                   String str=new String(disi.readLine());
                   dos.writeUTF(str);
                   System.out.println(str);
              }
         }
         catch(Exception e)
         {
              e.printStackTrace();
         }
     }

     public static void main(String args[])
     {
          try
          {
               clien c=new clien();
               c.talk();
          }
          catch(Exception e){ }
     }
}

There are tons of problems. 有很多问题。

It seems as if you're trying to do some kind of protocol like this: 似乎您正在尝试执行以下某种协议:

Client connects to server Client sends message to server Server receives message 客户端连接到服务器客户端向服务器发送消息服务器接收消息

A peer-to-peer type system. 对等类型的系统。 Not sure if you're expecting the server to be seen as another client (you type messages into it to send it to the client), but the problem is that right when the connection establishes, both Client and Server go into a loop. 不知道您是否希望服务器被视为另一个客户端(您在其中键入消息以将其发送给客户端),但是问题是,当建立连接时,客户端和服务器都陷入了循环。 In this loop, there's only 1 thing you can focus on. 在此循环中,您只能关注一件事。

Client: main(String[]) -> connect -> read input from user (loop) start program -> connect -> start listening for info from server 客户端:main(String [])->连接->从用户(循环)读取输入启动程序->连接->开始从服务器监听信息

Server: main(String[]) -> accept connection -> read input from user (loop) 服务器:main(String [])->接受连接->从用户读取输入(循环)

If you want your client to receive info from the server and be able to send info aswell, you need 2 threads. 如果您希望客户端从服务器接收信息并能够发送信息,则需要2个线程。

 static Socket s;
 static DataOutputStream out;
 static DataInputStream in;

 public static void main(String[] args) {
      try {
           s = new Socket("host", 2000);
           out = new DataOutputStream(s.getOutputStream());
           in = new DataInputStream(s.getInputStream());

           new Thread(new Runnable() {
                public void run() {
                     Scanner scanner = new Scanner(System.in);

                     String input;
                     while(!(input = scanner.nextLine()).equals("EXITPROGRAM")) {
                          out.writeUTF(input); //sends to client
                     }
                }
           }).start();

           while(true) {
                String infoFromServer = in.readUTF();

                //you can print to console if you want
           }
      }catch(Exception e) { }
 }

Now, this will allow the client to receive input from the user (from the console) AND receive data from the server aswell. 现在,这将允许客户端从用户(从控制台)接收输入,并从服务器接收数据。 You can use the same structure for your server aswell if that's what you're going for. 如果您要这样做,则也可以对服务器使用相同的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM