wood burning stoves*
The moose likes Applets and the fly likes Sockets! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Sockets!" Watch "Sockets!" New topic
Author

Sockets!

Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi All,
Can anybody explain simple application which give details how to connect client to server by socket methods? Mean how simply client can listen server sockets?
Thanks a lot in advance,
Angela
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi All,
Can anybody explain simple application which give details how to connect client to server by socket methods? Mean how simply client can listen server sockets? for example my server name is Angela and port number is 30)
Thanks a lot in advance,
Angela[/B]
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
Here's one of the cleanest and most common methods for creating a simple socket chat application.
http://homepages.unl.ac.uk/~wrightc/tyj/Comms.htm#ChatServer
As for the listening bit, let's have a look:

Basically, Java does all the work for you. You create a new ServerSocket and put it in an infinite loop (if you didn't loop it, then you would only 'hear' the first connection request). This particular type of Socket is designed to listen for connection requests of the port that you specify. Note that you should use a port number above 1024 as most of the port numbers below this value are reserved. When a request is made to the specified port, then this line is executed
Socket client = s.accept();
and you have the requesting client hooked up with the 'client' Socket. As you examin the code at the url that I noted above, notice the use of the static vector which allows all of the individual threads communicate with each other - very slick. I hope this helps
Sean
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi Sean,
Thanks a lot for your help! I have two questions here: In Socket method whether we have to write port number 10001 or also we have to write IP address and port number otherwise how client will come to know only on port number that which server to go.
ANd what is handler and purpose of it use:
chatHandler c = new chatHandler(client);
I checked the link you sent me. But i want to confirmed the following code you have written(not link) is enough to make only connection to server and how can I check whether client is connected to server or not without sending or receiving data?:
public class chatServer{
public chatServer() throws IOException
{ // initialise new ServerSocket
ServerSocket s = new ServerSocket(9999);
// loop listening for connections and
// instantiate a new handler for each one
while(true){
Socket client = s.accept();
chatHandler c = new chatHandler(client);
c.start();
}
}
public static void main(String[] args) throws IOException {
chatServer cs = new chatServer();
}
}
Please help me ,
Thanks in advance,
Angela
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
If you look at the code under the listing "A Chat Client (JDK1.1)" (just above the server code at the link I gave you) you'll notice that the client applet is hard-coded with the port number and the host server Ip address. You can (and should) pass this information in using the param tags of the applet tag in the html that launches the applet. This way, if you're server has to change, you don't have to re-compile the code.
The server code I pasted into the reply earlier is all that you need to accept client requests (pretty cool - a server with just three or so lines of code). The chatHandler class is simply a way of multi-threading the server so that, after the server gets a request, it creates a socket and passes it to a new chathandler (which runs as it's own thread for each client). All of the interaction with the client is contained in this class, leaving the server free to handle requests. If you analyse the three files (chatServer, chatHandler, chatClient) and gain an understanding of the algorithm, you'll have a solid understanding of the process. Best of luck!
Sean
Kaju Singh
Greenhorn

Joined: Jul 30, 2001
Posts: 10
Hi,
I tried to run the code given in the website referenced by you as it is. But the program is going into loops. I am using apache 1.3.20 in Windows aith apache JServ1.1.2. Also I am not able to run it thru' a browser. I am running it thru' the java run command.
I am new to this field. Though I have understood the logic, could you please guide me if I have to do any other settings. I am running the client program thru' htdocs directory and have kept the servlet in servlets directory of JServ module. I am able to connect properly, but when I press the send button, null is returned from the server
"Received from Server:null".
regards,
Kaju
Originally posted by Sean MacLean:
If you look at the code under the listing "A Chat Client (JDK1.1)" (just above the server code at the link I gave you) you'll notice that the client applet is hard-coded with the port number and the host server Ip address. You can (and should) pass this information in using the param tags of the applet tag in the html that launches the applet. This way, if you're server has to change, you don't have to re-compile the code.
The server code I pasted into the reply earlier is all that you need to accept client requests (pretty cool - a server with just three or so lines of code). The chatHandler class is simply a way of multi-threading the server so that, after the server gets a request, it creates a socket and passes it to a new chathandler (which runs as it's own thread for each client). All of the interaction with the client is contained in this class, leaving the server free to handle requests. If you analyse the three files (chatServer, chatHandler, chatClient) and gain an understanding of the algorithm, you'll have a solid understanding of the process. Best of luck!
Sean

 
Consider Paul's rocket mass heater.
 
subject: Sockets!
 
Similar Threads
Sending data from client to Server
Passing non serializable objects on sockets
What Platform/Java Technology - Please Help
remote server
Client-Socket-server!