• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 158
  • Last Modified:

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.








0
sraveend
Asked:
sraveend
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now