Is it a port problem?

Hi Folks!

I am new in java and working on a client server application. I am running server on port 5000. After that client connects to server on this port. Now after client connects I want to send 2 numbers to client to process. In my code I on server I am not able to send those 2 numbers to client. I am pretty sure something is wrong with the client constructor. HOW WILL I KNOW WHAT IS CLIENT PORT NUMBER IN ORDER TO CONNECT TO IT THROUGHT MY SERVER? If this is not the problem then what I am suppose to do?

Here is my code to give you guys a bit idea

SERVER CLASS

import java.io.*;

import java.net.*;

import java.util.*;

import java.util.StringTokenizer;

publicclass MathServer2extends Thread{

ServerSocket hi;

Socket client;

//for server

DataInputStream sbr;

DataOutputStream sdos;

//for client

DataInputStream br;

DataOutputStream dos;

int counter;

int x=33554393,i;

Integer a1,b1;

Stack st =new Stack();

publicstaticvoid main(String argv[])throws Exception{

new MathServer2();

}

public MathServer2()throws Exception{

hi =new ServerSocket(5000);

System.out.println("Server Listening on port 5000....");

this.start();

}

publicvoid run(){

System.out.println("Run start here...");

while(true){

try{

Socket client = hi.accept();

Connect c =new Connect(client);

System.out.println("Client is connected");

}catch(Exception e){}

}

}

}

class Connectextends Thread{

DataInputStream sbr;

DataOutputStream sdos;

Socket cs;

DataInputStream br;

DataOutputStream dos;

int x=33554393,i;

Integer a1,b1;

Stack st =new Stack();

public Connect(){}

public Connect(Socket s){

cs = s;

try{

br =new DataInputStream(cs.getInputStream());

dos =new DataOutputStream(cs.getOutputStream());

}catch(Exception e1){

try{

cs.close();

}catch(Exception e){System.out.println("EXCEPTION IN CLIENT. CLIENT CANNOT CONNECT...");}

return;

}

this.start();

}

publiclong add(long a,long b){

return a+b;

}

publicvoid run(){

try{

for(i=0;i<=x;i++)

{

if((x==0))

{

break;

}

else

x=x/10;

st.push(new Integer (x));

}

System.out.println("this is the fresh Stack..."+st);

if(st.empty() ==true)

{

// throw new EmptyStackException ( ) ;

System.out.println("Stack is empty..");

}

else

{

a1 = (Integer)st.pop();

b1 = (Integer)st.pop();

}

System.out.println("New Stack..."+st);

//sending 2 numbers to client.

cs =new Socket("herzing-g2eui8a", 3587);

System.out.println("testing 1...........");

sbr =new DataInputStream(cs.getInputStream());

sdos =new DataOutputStream(cs.getOutputStream());

System.out.println("testing 2...........");

long x,y;

x=a1.longValue();

sdos.writeLong(x);

System.out.println("testing 3...........");

dos.flush();

System.out.println("testing 4...........");

System.out.println("Server send client 1st # "+x);

y=b1.longValue();

sdos.writeLong(y);

sdos.flush();

System.out.println("Server send client 2nd# "+y);

System.out.println("I send 2 numbers "+x+" & "+y+" to client: "+cs+" for process");

//get answer back from client

String str = br.readLine();

System.out.println("I got it from clien: "+str);

long x1 = br.readLong();

System.out.println("I got: "+x1);

long y1 = br.readLong();

System.out.println("I got: "+y1);

Connect cc =new Connect();

long ans = cc.add(x1,y1);

Thread.currentThread().sleep (1000);

System.out.println("I am sending the answer..."+ans);

dos.writeBytes("The sum is: "+ans);

br.close();

dos.close();

cs.close();

}catch(Exception e){}

}

}

CLIENT CLASS

import java.io.*;

import java.net.*;

publicclass MathClient2{

publicstaticvoid main(String argv[])throws Exception{

Socket echo;

DataInputStream br;

DataOutputStream dos;

echo =new Socket("127.0.0.1", 5000);

br =new DataInputStream(echo.getInputStream());

dos =new DataOutputStream(echo.getOutputStream());

// int x = 5, y = 5;

long x = br.readLong();

dos.writeLong(x);

dos.flush();

System.out.println("Client send first number "+x);

long y=br.readLong();

dos.writeLong(y);

dos.flush();

System.out.println("Client send second number "+x);

//String str = br.readLine();

//System.out.println("Client Side.. I got: "+str);

}

}

Thanks a lot in advance

[9660 byte] By [asyed01a] at [2007-11-27 7:05:33]
# 1
You don't need to construct a new Socket in the server to talk to the client. You have already accepted one. Use that.
ejpa at 2007-7-12 18:56:47 > top of Java-index,Core,Core APIs...