Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

server/client classes

Status
Not open for further replies.

lhtimbo

Programmer
Sep 15, 2005
3
US
I apologize if this the wrong forum to post this question in.

Can anyone tell me how to change the client and server classes so that when I run them whatever is typed in the client window appears on the server window? As it is, it just prints "what's up" and ends. I need to be able to type, hit enter and have whatever I typed show up on the server window. Thanks for the help.

Code:
import java.net.Socket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.*;
//import java.lang.System;

public class Client
{
  public static void main (String args[]){
  	InetAddress ip=null;
  	Socket sock=null;
	try {
		ip = InetAddress.getLocalHost();
		sock = new Socket(ip, 2001);
	} 
	catch (UnknownHostException e1) {
		e1.printStackTrace();
	}
	catch (IOException e2) {
		e2.printStackTrace();
	}
	BufferedWriter dataOut=null;
    String message = "What's Up?\n";

    System.out.println ("Sending "+ message + " ...");
    try{
    	 dataOut = new BufferedWriter (new OutputStreamWriter (sock.getOutputStream ()));
    	 dataOut.write (message, 0, message.length ());
    	 dataOut.flush ();
    	 sock.close ();
    }
    catch(IOException e){
    	System.out.println(e.getMessage());
    }
  }
}

import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;

public class Server
{
  public static void main (String args[])
  {
    ServerSocket serverSock=null;
    Socket sock=null;
    BufferedReader dataIn=null;
    String message;

  	try{
  		serverSock = new ServerSocket (2001);
	}
  	catch (IOException e) {
		e.printStackTrace();
	}

    System.out.println ("Server starting ...");
    try {
		sock = serverSock.accept();
	}
    catch (IOException e1) {
		e1.printStackTrace();
	}

    try{
		dataIn = new BufferedReader(new InputStreamReader(sock.getInputStream()));
		message=dataIn.readLine();
		System.out.println (message);
	    sock.close ();
	}
    catch (IOException e2) {
        System.out.println (e2.getMessage());
	}



    try{
    	serverSock.close ();
	}
    catch(IOException e3) {
		e3.printStackTrace();
	}
  }
}
 
In the future, please post non-J2EE Java questions here : forum269 .

Code:
public class Client
{
  public static void main (String args[]){
      InetAddress ip=null;
      Socket sock=null;
    try {
        ip = InetAddress.getLocalHost();
        sock = new Socket(ip, 2001);
    } 
    catch (UnknownHostException e1) {
        e1.printStackTrace();
    }
    catch (IOException e2) {
        e2.printStackTrace();
    }
    BufferedWriter dataOut=null;
   
    // Get the argument from the programme start
    String message;
    if (args.length == 0) {
       System.out.println("no arguments supplied, using default message !";
        message = "default message\n";
     } else {
        message = args[0] +\n";
     }
    System.out.println ("Sending "+ message + " ...");
    try{
         dataOut = new BufferedWriter (new OutputStreamWriter (sock.getOutputStream ()));
         dataOut.write (message, 0, message.length ());
         dataOut.flush ();
         sock.close ();
    }
    catch(IOException e){
        System.out.println(e.getMessage());
    }
  }
}

Start the programme like this :

java Client "This is a message"

If you want to actaully read a message from the console while the programme is running, you'll need to use System.in .

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
sedj, thanks a lot, I really appreciate the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top