Can't get client and server to communicate

S

Srubys

hiya

The main problem is that server doesn’t receive any messages from
client( it receives a message from client only when I terminate the
client process ).
At first I thought that firewall was somehow preventing the two from
communicating, so I shut it down, but programs still couldn’t
communicate ( for the sake of readability I excluded try statements ):


SERVER SIDE:

public static void main(String[] args) {
Server.Init();
Server.handleClient();
}


class Server{
static int port = 2000;
static ServerSocket servs;
static Socket s;

static void Init(){
servs = new ServerSocket(port);
}

static void handleClient() throws IOException{

do{
Server s= new Server();
s.handleClient();
} while(true);

}

void handleClient() throws IOException{

s = servs.accept();
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bf = new BufferedReader(isr);

OutputStream os = s.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bfw = new BufferedWriter(osw);


String messages;
messages = bf.readLine();
System.out.println ( “this line won’t get written to the
console”);

while ( ! messages.equals("Stop") ){
bfw.write ( “this is what you’ve send me” + messages );
bfw.flush();
System.out.println( messages );
messages = bf.readLine();
}

s.close();
}

}


CLIENT SIDE:

static Socket s;
static String message, response;

public static void main(String[] args) {
send(1600);
}


static void send( int port ) {
InetAddress ia = InetAddress.getLocalHost();

s = new Socket( is, port );
OutputStreamWriter ow = new OutputStreamWriter(

s.getOutputStream(),"UTF8");
BufferedWriter bw = new BufferedWriter(ow);

InputStreamReader ir = new InputStreamReader(
s.getInputStream(),
"UTF8");
BufferedReader br = new BufferedReader(ir);

InputStreamReader console = new InputStreamReader(

System.in );
BufferedReader readConsole = new BufferedReader(

readConsole );


while ( message != "Stop" ){

message = readConsole.readLine();
bw.write(message);
bw.flush();

response = br.readLine(); // here the programs waits
and waits
and…
System.out.println ( " This was sent by server:");
System.out.println ( response );
}

s.close();


thank you
 
D

Donkey Hot

(e-mail address removed) wrote in (e-mail address removed):
hiya

The main problem is that server doesn’t receive any messages from
client( it receives a message from client only when I terminate the
client process ).
At first I thought that firewall was somehow preventing the two from
communicating, so I shut it down, but programs still couldn’t
communicate ( for the sake of readability I excluded try statements ):


SERVER SIDE:

public static void main(String[] args) {
Server.Init();
Server.handleClient();
}


class Server{
static int port = 2000;
static ServerSocket servs;
static Socket s;

static void Init(){
servs = new ServerSocket(port);
}

static void handleClient() throws IOException{

do{
Server s= new Server();
s.handleClient();
} while(true);

}

void handleClient() throws IOException{

s = servs.accept();

You like static stuff huh?

You have two handleClient() methods:
static void handleClient() throws IOException{
and

void handleClient() throws IOException{

Which one wins? I don't know, but I suggest you to get rid of those
static methods and member and try to be object oriented.
 
S

Srubys

Having two methods using same name was just a typo. My code doesn’t
have any meaningful names applied to methods, so when making this
thread, I tried to make code more readable, so I gave them more
appropriate names and in the process I accidentally gave identical
names to two of them.

I also noticed that ports on client side and server side differ ( one
is 1600 and other is 2000 ). I’m not sure how I managed to do that
when writing this thread, but actual code has same ports on client and
server side.




(e-mail address removed) wrote in (e-mail address removed):


The main problem is that server doesn’t receive any messages from
client( it receives a message from client only when I terminate the
client process ).
At first I thought that firewall was somehow preventing the two from
communicating, so I shut it down, but programs still couldn’t
communicate ( for the sake of readability I excluded try statements ):
SERVER SIDE:
public static void main(String[] args) {
Server.Init();
Server.handleClient();
}
class Server{
static int port = 2000;
static ServerSocket servs;
static Socket s;
static void Init(){
servs = new ServerSocket(port);
}
static void handleClient() throws IOException{
do{
Server s= new Server();
s.handleClient();
} while(true);

void handleClient() throws IOException{
s = servs.accept();

You like static stuff huh?

You have two handleClient() methods:
static void handleClient() throws IOException{
and

void handleClient() throws IOException{

Which one wins? I don't know, but I suggest you to get rid of those
static methods and member and try to be object oriented.


What would be the benefit of doing that, since it is a very simple and
short program?
 
D

Donkey Hot

(e-mail address removed) wrote in 34g2000hsf.googlegroups.com:
What would be the benefit of doing that, since it is a very simple and
short program?

How can I know that it is a simple and short program, when you do not post
it?

I would gladly try it in my IDE and see what can I do. If all I get is typo
filled news post, there is nothing I can do.

Nothing. Without the actual source code.
 
G

GArlington

hiya

The main problem is that server doesn’t receive any messages from
client( it receives a message from client only when I terminate the
client process ).
You mis-describe what is happening: I suspect that your server DOES
NOT receive any messages at all.
At first I thought that firewall was somehow preventing the two from
communicating, so I shut it down, but programs still couldn’t
communicate ( for the sake of readability I excluded try statements ):

The code will not work as it is for many reasons...

Few comments:
SERVER SIDE:

public static void main(String[] args) {
Server.Init();
Server.handleClient();
}

class Server{
static int port = 2000;
listening port is different from client's connecting port...
static ServerSocket servs;
static Socket s;

static void Init(){
servs = new ServerSocket(port);
}

static void handleClient() throws IOException{
assuming the above line reads: handleClient1()
do{
Server s= new Server();
s.handleClient();
1) you do not need to recreate Server all the time... handleClient()
is sufficient
} while(true);
2) you have NO WAY of exiting your server...
3) it is single threaded (i.e. server can handle only one client) in
case you intended to make it multi-threaded
void handleClient() throws IOException{

s = servs.accept();
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bf = new BufferedReader(isr);

OutputStream os = s.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bfw = new BufferedWriter(osw);

String messages;
messages = bf.readLine();
System.out.println ( “this line won’t get written to the
console”); Why do you think so?

while ( ! messages.equals("Stop") ){
bfw.write ( “this is what you’ve send me” + messages );
bfw.flush();
System.out.println( messages );
messages = bf.readLine();
}

s.close();
}

}

CLIENT SIDE:

static Socket s;
static String message, response;

public static void main(String[] args) {
send(1600);
4) Make sure that you are sending and receiving on the same port
}

static void send( int port ) {
InetAddress ia = InetAddress.getLocalHost();

s = new Socket( is, port );
5) What is "is"? It is NOT defined, did you mean "ia"?
OutputStreamWriter ow = new OutputStreamWriter(

s.getOutputStream(),"UTF8");
BufferedWriter bw = new BufferedWriter(ow);

InputStreamReader ir = new InputStreamReader(
s.getInputStream(),
"UTF8");
BufferedReader br = new BufferedReader(ir);

InputStreamReader console = new InputStreamReader(

System.in );
BufferedReader readConsole = new BufferedReader(readConsole );
6) was the above line meant to read: ... BufferedReader(console); ?
while ( message != "Stop" ){

message = readConsole.readLine();
bw.write(message);
bw.flush();

response = br.readLine(); // here the programs waits
and waits
and…
System.out.println ( " This was sent by server:");
System.out.println ( response );
}

s.close();

thank you

Generally the code is so unreadable that it is even hard to say what
it is trying to do.
Eventually, I did understand that the client is reading console input
and sending it to the server and the server echoes.
But, really, spend some time re-writing it, making the code readable
will make it easier to debug...
 
S

Srubys

On Jun 19, 7:56 pm, (e-mail address removed) wrote:> hiya


You mis-describe what is happening: I suspect that your server
DOES NOT receive any messages at all.

It does. If I terminate client program, then server does receive
client's message ( code I posted in this thread is full of typos ( I
tried to make code more readable for this thread and that is how the
typos creeped in), but not the actual program ).


I’m really sorry about so many typos in my code. I managed to get rid
of them and generally tried to make code more readable, though it
still isn’t perfect. So here are the two programs ( hopefully, you
will be able forgive my sloppiness and help me some more ):

Still, the two processes should be able to exchange messages, since
however ugly the code is, it is correctly written non the less.


CLIENT SIDE:

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


public class Main{
static Socket s;
static String message, response;

public static void main(String[] args) {
try{
send( 2000 );
}
catch(Exception e){ e.printStackTrace(); }
}

static void send( int port ) throws IOException{
InetAddress ia = InetAddress.getLocalHost();

s = new Socket( ia, port );

OutputStreamWriter ow = new OutputStreamWriter(
s.getOutputStream(),"UTF8");
BufferedWriter bw = new BufferedWriter(ow);


InputStreamReader ir = new InputStreamReader(
s.getInputStream(),"UTF8");
BufferedReader br = new BufferedReader(ir);


InputStreamReader console = new InputStreamReader(
System.in );
BufferedReader readConsole = new BufferedReader(
console );


do
{
message = readConsole.readLine();
bw.write(message);
bw.flush();
System.out.println("waiting for a response");
response = br.readLine();
System.out.print( " This was sent by server:");
System.out.println ( response );
} while ( !message.equals("Stop") );

s.close();
}
}


SERVER SIDE:

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


public class Main {

public static void main(String[] args) {
try{
Server.Init();
Server.handleClient();
}
catch(Exception e){ e.printStackTrace(); }
}
}


class Server{
static int port = 2000;
static ServerSocket servs;
static Socket s;

static void Init() throws IOException{
servs = new ServerSocket( port );
}


static void handleClient() throws IOException{
do{
Server.connect();
} while(true);

}



static void connect() throws IOException{

s = servs.accept();
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bf = new BufferedReader(isr);

OutputStream os = s.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bfw = new BufferedWriter(osw);

String messages;
System.out.println("reading first message");
messages = bf.readLine();
System.out.println("first message read");

while ( ! messages.equals("Stop") ){
bfw.write ( "message received" );
bfw.flush();
System.out.println( messages );
messages = bf.readLine();
}

s.close();
}

}


Eventually, I did understand that the client is reading console
input and sending it to the server and the server echoes.

correct

Again, I'm sorry for being so sloppy. I know that is one quality a
programmer should not have and I'm trying to change that and become
more organized, but it isn't easy!
 
N

Nigel Wade

It does. If I terminate client program, then server does receive
client's message ( code I posted in this thread is full of typos ( I
tried to make code more readable for this thread and that is how the
typos creeped in), but not the actual program ).


I’m really sorry about so many typos in my code. I managed to get rid
of them and generally tried to make code more readable, though it
still isn’t perfect. So here are the two programs ( hopefully, you
will be able forgive my sloppiness and help me some more ):

Still, the two processes should be able to exchange messages, since
however ugly the code is, it is correctly written non the less.


CLIENT SIDE:
OutputStreamWriter ow = new OutputStreamWriter(
s.getOutputStream(),"UTF8");
BufferedWriter bw = new BufferedWriter(ow);


InputStreamReader ir = new InputStreamReader(
s.getInputStream(),"UTF8");
BufferedReader br = new BufferedReader(ir);


InputStreamReader console = new InputStreamReader(
System.in );
BufferedReader readConsole = new BufferedReader(
console );


do
{
message = readConsole.readLine();
bw.write(message);
bw.flush(); [...]



static void connect() throws IOException{

s = servs.accept();
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bf = new BufferedReader(isr); [...]
messages = bf.readLine();
Eventually, I did understand that the client is reading console
input and sending it to the server and the server echoes.

correct

From an initial scan of the code the problem appears to be your use of
readers/writers. In the client you use a BufferedReader.readLine() to read a
line from the console. This returns a String which *does not* include the line
terminator. You then send this to the server using BufferedWriter.write() which
AFAIK does not append any line terminator for you. The server reads it with a
BufferedReader.readLine(). However, since your write doesn't send any line
terminator the readLine() in the server will not return - until the client
closes the socket when it terminates.
Again, I'm sorry for being so sloppy. I know that is one quality a
programmer should not have and I'm trying to change that and become
more organized, but it isn't easy!

If you write working code, then cut and paste it into the message you solve the
problem. You send us the code you are actually using, which does not work,
rather than some potted version you've typed into the message which won't even
compile.
 
S

Srubys

thanx, it works now alright.
If you write working code, then cut and paste it into the message you solve the
problem. You send us the code you are actually using, which does not work,
rather than some potted version you've typed into the message which won't even
compile.

Yeah, I should have done that in the first place. But I did post
code I was actually using in my second post.

thank you

cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top