socket programming

C

countavdhesh

hi guys
I am developing a chat server in java.I programmed client and server.
The client program has two threads one for getting input from keyboard
and second one for reading data from server.

My client program runs sucessfully.
But i am not able to program server correctly.

plz help me.
thanks in advance
 
C

count

actually clients sucessfully connect with the server.

The server program, listens for connections and each new connection is
spawned into a new thread (a user object).

server is not able send message to every clients.
it only sends message to same client.

this my problem
 
G

Gordon Beaton

I am developing a chat server in java.I programmed client and
server. The client program has two threads one for getting input
from keyboard and second one for reading data from server.

My client program runs sucessfully.
But i am not able to program server correctly.

How do you know the client is correct, when you don't have a working
server?

What specific problems are you having? Or were you hoping someone
would just provide you with a working server that your existing client
will work with?

There is a tutorial and example code here:
http://java.sun.com/docs/books/tutorial/networking/index.html

In particular, there is a simple client/server described under "all
about sockets".

/gordon
 
S

shakah

count said:
actually clients sucessfully connect with the server.

The server program, listens for connections and each new connection is
spawned into a new thread (a user object).

server is not able send message to every clients.
it only sends message to same client.

this my problem

Does your server-side "user object" use a static variable for the
connected Socket?
 
N

Nigel Wade

count said:
actually clients sucessfully connect with the server.

The server program, listens for connections and each new connection is
spawned into a new thread (a user object).

server is not able send message to every clients.
it only sends message to same client.

this my problem

You need to provide a mechanism within the server to allow the different
user objects to communicate with each other, and thereby the other clients.
 
C

count

here is my client program



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


public class client
{

public static void main(String args[])
{
Socket cli = new Socket("localhost ",6789); \\6789 is port
number
new readmessagefromkeyboard(cli); \\class for reading
message from keyboard
new readmessagefromserver(cli);
}
}


public class reamessagefromkeyboard implements Runnable
{
String s;
BufferedReader ink;
BufferedWriter outs;
Thread t;

public readmessagefromkeyboard(Socket cli )
{
ink=new BufferedReader(new
InputStreamReader(cli.getInputStream()));
outs=new BufferedWriter(new
OutputStreamWriter(cli.getOutputStream()));
t=new Thread(this);
t.start();
}

public void run()
{
try
{
while(true)
{
s=ink.readLine();
outs.write(s +'\n');
outs.flush();
}
}

catch(Exception e)
{
}


}
}
}


public class readmessagefromserver implements Runnable
{
BufferedReader ins;
String msg;
Thread t1;

public readmessagefromserver(Socket cli)
{
ins=new BufferedReader(new
InputStreamReader(cli.getInpoutStream()));
}

public void run()
{
while(true)
{
msg=ins.readLine();
System.out.println("message" + msg);
//here is code to read message from server
}

}
}

}



Is this client program is correct.
Thanks in advance
 
H

HK

hi guys
I am developing a chat server in java.I programmed client and server.
The client program has two threads one for getting input from keyboard
and second one for reading data from server.

My client program runs sucessfully.
But i am not able to program server correctly.

<shameless_plug>
You could give

http://www.ebi.ac.uk/Rebholz-srv/whatizit/monq-doc/monq/net/TcpServer.html

a try. I handles all the nasty bits of accepting
connections, caring for exceptions, spawning
threads for connections and cleaning up
after them. The only thing you have to provide
yourself is a class which, in its run() method,
reads from an InputStream (from the client)
and writes to an OutputStream (to the client).

In
http://www.ebi.ac.uk/Rebholz-srv/whatizit/monq-doc/monq/stuff/Pipe.html
you may find help to set up the feeding and
draining threads for the client.

The software can be downloaded from
http://www.ebi.ac.uk/Rebholz-srv/whatizit/software
</shameless_plug>

Harald.
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top