• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sockets!

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
The City calls upon her steadfast protectors. Now for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic