Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Socket Programming

  1. #1
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Socket Programming

    I have been going through java networking tutorials though not sure what to connect to, e.g. If I'm on my laptop what can I connect to that will give me permission, I don't want to connect to something I shouldn't. tutorials don't give real IP address and ports that would help me practice so not sure what to do.

    I know it's a stupid question, would appreciate any replies.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Socket Programming

    If you're just doing some testing, use your localhost address: 127.0.0.1. This way you can safely create server/client connections on just your machine.

    If you want to connect to another device, you'll need to find the IP address of that device. Google can help you find the IP address of another computer (for example, "find IP address on Windows", or Linux, etc.). I would recommend doing basic testing on just your local network as connections across the internet start to get tricky to setup. Keep in mind many networks use DHCP (dynamic IP addresses) so your IP address may change (particularly when a device disconnects/reconnects).

    Which port to use depends on your application. Wikipedia Has a nice list of ports which are "officially" registered, but you can re-use ports even if they are "registered. For example, if you're writing an HTTP server you'll likely want to use port 80 because this is the default HTTP port. A good rule of thumb I use if my application is still in early development/for a specialized purpose is pick a port above 1023 (0-1023 on some systems require superuser/admin privileges), like 8000 (8000 is actually not that great a choice because so many programs use it). If it doesn't work, increment by 1 and try again (so 8001, 8002, etc.).

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    keepStriving (May 31st, 2013)

  4. #3
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Socket Programming

    Thank you, I'm still somewhat confused though have an outline of what you mean, I still need practice writing java program's though I'm finding networking quite interesting so I'm getting sidetracked.I'll keep trying, if you know any good networking resources that you have found helpful, specific to java other than sun tutorials please do recommend.

    --- Update ---

    Local host didn't work neither so very confused now, do you think I should learn to setup a network so I can get a better understand of how it works as now I'm clueless, though I don't want to end up screwing up my Internet or phone connection.

  5. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Socket Programming

    In that case post the code you are having problems with (or a shortened version). Something is out of order somewhere it seems.

  6. #5
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Socket Programming

    I found the following code on the net from a person who got it working on his local host though not on ip.
    import java.io.*;
    import java.net.*;
     
     
    public class ServerTest{
    	public static void main(String[] args){
    		final int PORT_NUMBER = 8089;
    		while(true){
    			try{
    				ServerSocket serversock = new ServerSocket(PORT_NUMBER);
    				System.out.println("LITSENING.....");
     
    				Socket clientSock = serversock.accept();
    				System.out.println("Connected Client");
    				BufferedReader br = new BufferedReader(new InputStreamReader(clientSock.getInputStream()));
    				System.out.println(br.readLine());
    				br.close();
    				serversock.close();
    				clientSock.close();
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    	}
     
    }
    import java.net.*;
    import java.io.*;
    public class ClientTest{
      public static void main(String [] args)throws IOException{
    	   final int PORT_NUMBER = 8089;
    	   final String HOSTNAME = "127.0.0.1";
    	   try{
    		   Socket sock = new Socket(HOSTNAME,PORT_NUMBER);
    		   PrintWriter out = new PrintWriter(sock.getOutputStream(),true);
    		   out.println("test");
    		   out.flush();
    		   out.close();
    		   sock.close();
     
    	   }catch(Exception e){
    		   e.printStackTrace();
    	   }
      }
    }
    I can't get it working on local or otherwise.

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Socket Programming

    What problems are you running into? I tested it and it works fine for me. Make sure you start the server before trying to run the client.

    I've typically found testing using localhost is the easiest.

  8. #7
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Socket Programming

    I've got the exercise on sun networking tutorial to work so now need to spend more time on how it works. Is there any way I can explore my network connection similar to exploring files by opening, closing, creating and deleting directories etc. I can't seem to find anything online saying that this can be done so not sure, it would give me more of an understanding of my network and how it works.
    I took note of how you said run server first then client, Thank you.

  9. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Socket Programming

    One program I've used in the past is WireShark, but generally I prefer to use the "standard" Java debug tools, i.e. Eclipse, Netbeans, whatever you use for debugging Java programs. I check the data the source is sending, and then I check for how the receiver is interpreting the data. It's assumed that the data is being transmitted correctly (otherwise there are problems in Java or your network, most likely there aren't), and the only important places to check are the source and destination. Analyzing the data using debug tools is simply much easier than using a packet analyzer like WireShark.

  10. #9
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    My Mood
    Bored
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Socket Programming

    In java, two sockets can be connected by client and server programming. Server socket in server side listens to the TCP port and the client sends the data request through this socket.

Similar Threads

  1. Regarding socket programming
    By Dnyaneshwar in forum Java Networking
    Replies: 1
    Last Post: March 5th, 2013, 03:19 AM
  2. Socket Programming over the Internet
    By djmaxtor in forum Java Networking
    Replies: 6
    Last Post: July 24th, 2012, 04:21 AM
  3. Socket Programming and GUI
    By oyunmax in forum Java Networking
    Replies: 3
    Last Post: June 15th, 2011, 03:01 PM
  4. socket programming
    By advjava in forum Member Introductions
    Replies: 3
    Last Post: April 5th, 2011, 11:21 PM
  5. [SOLVED] Problem in socket programming
    By sinni in forum Java Networking
    Replies: 1
    Last Post: March 15th, 2011, 10:26 AM