java programming in network

Server program..................................



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


public class lab8S
{
public static final void main(String[] args)
{

while(true){
try{
ServerSocket ss=new ServerSocket(2002);
new process1(ss.accept()).start();
try
{
Thread.currentThread().sleep(100);
}catch(InterruptedException ie)
{
}

}
catch(IOException e)
{
}
}
}
}
class KeyboardInThread extends Thread {
      PrintWriter pout = null;
      OutputStream out;
      BufferedReader sIn = new BufferedReader(new InputStreamReader(System.in));
    public KeyboardInThread(OutputStream out) {
            this.out = out;
    }

      public void run() {
while(true) {

                  try {
                        pout = new PrintWriter(out, true);
                        pout.println(sIn.readLine());
                        pout.flush();

                  }catch(IOException ioe) {
                        }
                  }
      }
}
class ScreenOutThread extends Thread {
      InputStream in;
      BufferedReader br=null;
    public ScreenOutThread(InputStream inB) {
            in = inB;
    }
      public void run() {
            while(true) {

               try {
                        br=new BufferedReader(new InputStreamReader(in));
                        System.out.println(br.readLine());
                        }catch(Exception ioe) {}}
      }
}

class process1 extends Thread
{
private Socket s;
public process1(Socket soc)
{
s=soc;
}

public final void run()
{
while(true)
{
try{
new ScreenOutThread(s.getInputStream()).start();
new KeyboardInThread(s.getOutputStream()).start();
try{
Thread.currentThread().sleep(1000);
}catch(InterruptedException e)
{}
}
catch(Exception e)
{
try{
s.close();
}
catch(Exception ex){}
return;
}
}
}
}


Client program

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


public class dlab8c
{
public static final void main(String[] args)
{


try{
Socket eSocket = null;
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter address of a server");
eSocket = new Socket(InetAddress.getByName(stdIn.readLine()),2002);
new proc(eSocket).start();
try
{
Thread.currentThread().sleep(100);
}catch(InterruptedException ie)
{
}

}
catch(IOException e)
{
}
}
}
class KThread extends Thread {
      PrintWriter pout = null;
      OutputStream out;
      BufferedReader sIn = new BufferedReader(new InputStreamReader(System.in));
    public KThread(OutputStream out) {
            this.out = out;
    }

      public void run() {
while(true) {

                  try {
                        pout = new PrintWriter(out, true);
                        pout.println(sIn.readLine());
                        pout.flush();

                  }catch(IOException ioe) {
                        }
                        }
      }
}
class SThread extends Thread {
      InputStream in;
      BufferedReader br=null;
    public SThread(InputStream inB) {
            in = inB;
    }
      public void run() {
            while(true){
               try {
                        br=new BufferedReader(new InputStreamReader(in));
                        System.out.println(br.readLine());

                        }catch(Exception ioe) {}
                        }
      }
}

class proc extends Thread
{
private Socket s;
public proc(Socket soc)
{
s=soc;
}

public final void run()
{
while(true)
{
try{
new KThread(s.getOutputStream()).start();
new SThread(s.getInputStream()).start();
try{
Thread.currentThread().sleep(100);
}catch(InterruptedException e)
{}
}
catch(Exception e)
{
try{
s.close();
}
catch(Exception ex){}
return;
}
}
}
}

Please help to make the above server and client program to work properly.It just shows communication between server and client.








sraveendAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodingExpertsCommented:
//server program ............................
import java.net.*;
import java.io.*;

public class tcpServer {
   
    public static void main(String args[]) {
      
      int port;
      ServerSocket server_socket;
      BufferedReader input;
      
      try {
          port = Integer.parseInt(args[0]);
      }
      catch (Exception e) {
          System.out.println("port = 2200(default)");
          port = 2200;
      }

      try {
          
          server_socket = new ServerSocket(port);
          System.out.println("Server waiting for client on port " +
                         server_socket.getLocalPort());
          
          // server infinite loop
          while(true) {
            Socket socket = server_socket.accept();
            System.out.println("New connection accepted " +
                           socket.getInetAddress() +
                           ":" + socket.getPort());
            input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            // print received data
            try {
                while(true) {
                  String message = input.readLine();
                  if (message==null) break;
                  System.out.println(message);
                }
            }
            catch (IOException e) {
                System.out.println(e);
            }
            
            // connection closed by client
            try {
                socket.close();
                System.out.println("Connection closed by client");
            }
            catch (IOException e) {
                System.out.println(e);
            }
            
          }
      }
      
      catch (IOException e) {
          System.out.println(e);
      }
    }
}
---------------------------------------------------------------------------

// tcpClient.java
// usage : java tcpClient <server> <port>
// default port is 2200

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

public class tcpClient {
    public static void main(String[] args) {

      int port = 2200;
      String server = "localhost";
      Socket socket = null;
      String lineToBeSent;
      BufferedReader input;
      PrintWriter output;
      int ERROR = 1;
      
      // read arguments
      if(args.length == 2) {
          server = args[0];
          try {
            port = Integer.parseInt(args[1]);
          }
          catch (Exception e) {
            System.out.println("server port = 2200(default)");
            port = 2200;
          }
      }
            
      // connect to server
      try {
          socket = new Socket(server, port);
          System.out.println("Connected with server " +
                           socket.getInetAddress() +
                           ":" + socket.getPort());
      }
      catch (UnknownHostException e) {
          System.out.println(e);
          System.exit(ERROR);
      }
      catch (IOException e) {
          System.out.println(e);
          System.exit(ERROR);
      }
      try {
          input = new BufferedReader(new InputStreamReader(System.in));
          output = new PrintWriter(socket.getOutputStream(),true);
          
          // get user input and transmit it to server
          while(true) {
            lineToBeSent = input.readLine();
            // stop if input line is "."
            if(lineToBeSent.equals(".")) break;
            output.println(lineToBeSent);
          }
      }
      catch (IOException e) {
          System.out.println(e);
      }

      try {
          socket.close();
      }
      catch (IOException e) {
          System.out.println(e);
      }
    }    
}

            
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NaeemgCommented:
r u going to make chat server application?
0
sraveendAuthor Commented:
yes
0
sraveendAuthor Commented:
The server and client will need to be threaded in such a way that they can support multiple simultaneous clients  and i want to tie the server and client together in such a way that any time the server executes a out.println(); it prints to the client's screen, and any time the server executes a in.readLine() method, it is reading from the client's keyboard
0
CodingExpertsCommented:
didn't the code i provided suffice your needs !!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.