Results 1 to 3 of 3
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By Tolls

Thread: basics for creating a socket

  1. #1
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default basics for creating a socket

    I've created a socket for client, and a ServerSocket, and a socket for server, and when I run the code, it stops and atlast shows "timed out" shows Connection refused error

    Java Code:
    import java.io.*;
    import java.net.*;
    
    	public class socketBasics{
    		static Socket client;
    		static Socket server;
    		static ServerSocket serverSo;
    		static int errorCount=0;
    		public static void main(String args[])
    		{
    			try {
    				client=new Socket("localhost",100);
    				serverSo=new ServerSocket(100);
    				serverSo.setSoTimeout(1000);
    				server=serverSo.accept();
    				if(server.isConnected())
    					System.out.println("address="+client.getRemoteSocketAddress()+"server="+server.getRemoteSocketAddress());
    				else 
    					System.out.println("timeout="+serverSo.getSoTimeout());
    				}catch(Exception err){System.out.println("exception#"+errorCount+":"+err);errorCount++;}
    
    		}
    	}
    output:
    Java Code:
    exception#0::java.net.ConnectException: Connection refused: connect
    why is client not getting connected?
    thanks in advance
    Last edited by noobplus; 10-14-2013 at 05:09 PM. Reason: compacting the alignment

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: basics for creating a socket

    What do you expect to happen?
    Your client and server sockets are on the same thread.
    Your client socket will sit there waiting for the server on that port to reply...except the server socket hasn't even been created, so the exception is thrown.

    If you move the client creation to after the server creation then the server will sit there in the accept() method waiting for a client...and will then timeout as the client doesn't exist.

    You need them to be on separate threads.
    noobplus likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: basics for creating a socket

    Launch the server on a separate thread.

    The easiest would be to write them as separate classes.
    After all, they are separate things.
    noobplus likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 16
    Last Post: 08-08-2013, 04:34 PM
  2. creating a socket and connecting to host
    By droidus in forum New To Java
    Replies: 18
    Last Post: 12-13-2011, 07:04 PM
  3. Replies: 2
    Last Post: 02-16-2011, 04:30 AM
  4. Creating Socket
    By ersachinjain in forum Networking
    Replies: 1
    Last Post: 11-18-2009, 10:10 PM
  5. Creating Socket Issue
    By castiel in forum Networking
    Replies: 2
    Last Post: 08-25-2009, 01:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •