Sockets
Hi,
I just finished reading through the java networking tutorial, and I want to create a program that lets two computers communicate with eachother. I don't have two computers to test it on at the moment, so I thought I might ask here to make sure. For two people to communicate, one person would open up a ServerSocket:
ServerSocket serverSocket =new ServerSocket(1234);
And the other would open up a regular Socket:
Socket s =new Socket("?", 1234);
Is this the correct way of doing this? I'm not really sure what is supposed to replace the "?", but I will do some more research right now to try and figure it out. Thanks ahead of time.
Any helpful links will also be appreciated.
Message was edited by:
Bobert.
[863 byte] By [
Bobert.a] at [2007-11-26 15:03:09]

Look at the Socket API: http://java.sun.com/j2se/1.5.0/docs/api/java/net/Socket.htmlYou can read all about it there.
> I'm not really sure what is supposed to replace the "?",
What does the API documentation say? Its the IP address of the server.
If you don't have two computers then you can run the client and the server on the same computer. In this case you can use "127.0.0.1" as the IP address. Its a special IP that points back to the current computer.
> > I'm not really sure what is supposed to replace the
> "?",
>
> What does the API documentation say? Its the IP
> address of the server.
Thanks, I just re-read that in the tutorial. Sorry if this is a stupid question, but how is the client supposed to know the server's IP address?
> If you don't have two computers then you can run the
> client and the server on the same computer. In this
> case you can use "127.0.0.1" as the IP address. Its a
> special IP that points back to the current computer.
Thanks again, camickr.
That depends on the system design.Usually the user that uses the client will enter the address of the server.
> That depends on the system design.
> Usually the user that uses the client will enter the
> address of the server.
This is what I am trying to do. I made a game and I want to be able to play with other people on other computers. I was hoping there was a way that someone could start a game and wait for someone else to join it. I have seen games where, when starting a new game, ask you to input your name. Other people can then search for that name and enter that game. Does that make sense? Is there a way to do that? Thanks again.
> but how is the client supposed to know the server's IP address?Well don't forget to read the API. You don't have to specify a String. You can specify another type of Object. And this type of Object will allow you to resolve "www.abc.com" Strings to an IP address.
> I want to be able to play with other people on other computers
Unless your computer has a permanent IP address this obvously won't work. Every time you reboot your computer and connect to the internet you have a new IP address so you will need to call you friend to tell them what the address currently is.
You can keep a file with the IP address of your computer on a free site like geocities, where you have a permanent address.Your clients will have to read the file and parse it to get the server IP address, and you will have to update this file every time you reboot.
> You can keep a file with the IP address of your
> computer on a free site like geocities, where you
> have a permanent address.
> Your clients will have to read the file and parse it
> to get the server IP address, and you will have to
> update this file every time you reboot.
But this won't work if I change the computer. I want it to work if anyone gets on and plays, not just me and another person. There's no way to do this in java? I found an open-source java messenger, maybe I'll see how they did it. Thanks.
> There's no way to do this in java?
It has nothing to do with Java. This is the way the internet works. Computers talk to one another by IP addresses.
When you go to "forurm.java.sun.com" the domain name gets converted to an IP address. This can only be done if the domain name is registered. You internet provide has a DomainNameServer (DNS) which looks up the IP address and then forwards the request to that computer. This functionality is built in to your browser to its hidden from you.
> I want it to work if anyone gets on and plays, not just me and another person.
Thats what a web site is for. You register a domain name and host your application. Of course you will need a host provider that supports Java.
So, could I create a file on a free website and write the IP address to that everytime anyone from any computer plays? That way, the next person to play can just read that IP address and connect to it, right?Thanks for bearing with me, BTW.
1) you can as well use something like dyndns.org. Your PC registers there after booting, and they will forward any request to "bobert.dyndns.org" to your PC, regardless of its current IP address.
2) instant messengers have a central server with a known IP they connect to and register with. Then they ask this server who else is online. The server then does the communication "linking".
> 1) you can as well use something like dyndns.org.
> Your PC registers there after booting, and they will
> forward any request to "bobert.dyndns.org" to your
> PC, regardless of its current IP address.
Could you elaborate a bit more please? I've never heard of this before.
Hi, could you post the link to the tutorial your reading about sockets? Im doing a chat program my self I wanna know more on how to do it.
http://java.sun.com/docs/books/tutorial/networking/TOC.html
> could you post the link to the tutorial your reading about sockets? [url http://java.sun.com/docs/books/tutorial/]Custom Networking[/url]Next time I should learn to read the postings on page 2 before postingMessage was edited by: camickr