Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Passing Port Number Between Client And Server Using Command line interface

843790Mar 23 2008 — edited Mar 23 2008
Hi there,

I am currently building a basic hybrid CLI chat program for my network hmw.I am new to network programmin and I have a huge way to go on..The program has to have a client and server side ofcource and it has to take these things port numbers from commandline(dos).

The problem is after I have succesfully compiled and executed the server,when I try to execute the client from commandline I am unable to connect the client to it because it loses the server's port number.Does anybody know a way to get servers port name(which is a commandline parameter given during execution) and pass it to mysocket correctly so I can dynamically change the connection ports of clients and servers as requested for the homework?


Here is my code for server:
/**
* @(#)server.java
*
*
* @author
* @version 1.00 2008/3/22
*/
 
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
 
public class server {
public static int port;
static int connectionCount = 0;
public static final int DEFAULT_PORT = 9800;
public static void main(String[] args) {
int defaultport = DEFAULT_PORT;
ServerSocket serverSocket = null;
Socket socket = null;
try {
if(args.length > 0&&isvalid(port))
{
port = Integer.parseInt(args[0]);
System.out.println(port);
}
else
{
port = defaultport;
}
} catch(NumberFormatException nfe) {
System.err.println("Usage: java ChatServer [port]");
System.err.println("Where options include:");
System.err.println("\tport the port on which to listen.");
System.exit(0);
}
try {
//Bunu gerektiği zaman yapacak.
ServerSocket server1 = new ServerSocket(port);
System.out.println("Server socket created");
while(true) {
Socket client1 = server1.accept();
System.out.println("Accepted from "+client1.getInetAddress());
chatlistener gozlem=new chatlistener(client1);
gozlem.run();
/*
socket = serverSocket.accept();
client handler = new client(socket);
*/
connectionCount++;
System.out.println("Current number of clients connected to server is"+connectionCount);
System.out.println("Connection established.");
//handler.start();
}
} catch(IOException ioe) {
ioe.printStackTrace();
} finally {
try {
serverSocket.close();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
public static int getportNo()
{
return port;
}
 
public static boolean isvalid(int port)
{
if(port>65536&&port<1)
{
return false;
}
return true;
}
 
}
Here is my code for client:
 
/**
* @(#)client.java
*
*
* @author
* @version 1.00 2008/3/22
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Vector;
 
public class client extends Thread {
int clientnumber = 0;
static server myserver= new server();
static int portNo = myserver.getportNo();
static int btwport;
static Vector handlers = new Vector( 10 );
static Socket socket;
static BufferedReader in;
static PrintWriter out;
static int listeningport=portNo;
static String nickname;
static String hostname;
 
public static void main(String args[]){
int emmme;
 
try {
if(args.length > 0)
{
nickname = args[0].toString();
hostname = args[1].toString();
btwport = Integer.parseInt(args[2]);
 
}
else
{
System.out.println("You must specify 3 parameters. nickname(string) hostname(string) port(integer)va");
}
}
catch(Exception e) {
e.printStackTrace();
System.out.println("Our input argument must consist of nickname(string),hostname(string),port(integer)");
System.out.println("Please beware of format exceptions and do not forget to specify all fields.");
}
finally
{
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println(hostname);
 
System.out.println(listeningport);
Socket mysocket = new Socket(hostname,listeningport);//Portunu set edebilirsin.
//Here it gives an error because it does not gte listening port correctly
//
//
//
System.out.println("123");
//BufferedReader in = new BufferedReader(
//new InputStreamReader(mysocket.getInputStream()));
PrintWriter out = new PrintWriter(
new OutputStreamWriter(mysocket.getOutputStream()));
out.print("Client"+nickname+"connected to the server");
//out.close();
System.out.println("3");
System.out.println("Client deployed");
}catch(Exception e)
{
e.printStackTrace();
}
}
}
public void run() {
System.out.println("Client runs");
String line;
synchronized(handlers) {
handlers.addElement(this);
 
}
try {
while(!(line = in.readLine()).equalsIgnoreCase("/quit")) {
for(int i = 0; i < handlers.size(); i++) {
synchronized(handlers) {
client handler =
(client)handlers.elementAt(i);
handler.out.println(line + "\r");
handler.out.flush();
}
}
}
} catch(IOException ioe) {
ioe.printStackTrace();
} finally {
try {
in.close();
out.close();
socket.close();
} catch(IOException ioe) {
} finally {
synchronized(handlers) {
handlers.removeElement(this);
}
}
}
}
}
Any help would be appreciated.How can I pass the servers port which I get from the command line interface correctly to the client after executing the server?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 20 2008
Added on Mar 23 2008
1 comment
593 views