简体   繁体   中英

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...

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

Server: main(String[]) -> accept connection -> read input from user (loop)

If you want your client to receive info from the server and be able to send info aswell, you need 2 threads.

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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