Results 1 to 4 of 4
  1. #1
    Atriamax is offline Member
    Join Date
    Sep 2009
    Posts
    41
    Rep Power
    0

    Default Simple Socket IOstream problem

    ok, I decided to start trying to network for the first time, so I looked up the tutorial and tried out some of the code. I kept running into the IOException that was thrown. I tried to fix it and eventually i just copied and pasted the code into my compiler and I still get the same error caught.

    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class EchoClient {
        public static void main(String[] args) throws IOException {
    
            Socket echoSocket = null;
            PrintWriter out = null;
            BufferedReader in = null;
    
            try {
                echoSocket = new Socket("Alexs-Computer.local", 4321);
                //echoSocket = new Socket("Alexs Computer", 4321); 
                //echoSocket = new Socket(InetAddress.getLocalHost.getHostName, 4321);
                //echoSocket = new Socket("10.0.1.5", 4321); 
                out = new PrintWriter(echoSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(
                                            echoSocket.getInputStream()));
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host.");
                System.exit(1);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for "
                                   + "the connection");
                System.exit(1);
            }
    
    	BufferedReader stdIn = new BufferedReader(
                                       new InputStreamReader(System.in));
    	String userInput;
    
    	while ((userInput = stdIn.readLine()) != null) {
    	    out.println(userInput);
    	    System.out.println("echo: " + in.readLine());
    	}
    
    	out.close();
    	in.close();
    	stdIn.close();
    	echoSocket.close();
        }
    }
    It runs into the IO exception so outputs "Couldn't get I/O for the connection"

    Can anyone help me out? Im just trying to get it to run so I can move on!

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    maybe "Alexs-Computer.local" is not in the hosts file, or somehow not DNS resolveable into an IP address. Try using your IP address for the machine that the server is running on (e.g. 127.0.0.1)

    Also, this is the client that connects to a server right, would you have a server process running and listening on TCP port 4321 in this case ?

  3. #3
    Atriamax is offline Member
    Join Date
    Sep 2009
    Posts
    41
    Rep Power
    0

    Default

    Alexs-Computer.local I think works too, and i did try 10.0.1.5, I put comments of the other host names, same results for all of them.

    So is the problem that I need a server? I didnt know I server for this kind of thing, I was trying to get the basics down. The next section was a server but I didn't want to go on until I got this working!

    Edit: Well, I got code for the server from the same place and it too runs into an IOException! Is it something wrong with my computer being able to be a host?
    Last edited by Atriamax; 12-01-2009 at 07:51 AM.

  4. #4
    Atriamax is offline Member
    Join Date
    Sep 2009
    Posts
    41
    Rep Power
    0

    Default

    Alright I got it all figured out. I had the right name and everything just had to piece it together differently.

Similar Threads

  1. Problem with Java Heap Space using Socket
    By mvillara in forum Networking
    Replies: 3
    Last Post: 08-21-2009, 04:45 AM
  2. [SOLVED] problem in socket connection
    By neeraj.singh in forum Networking
    Replies: 2
    Last Post: 02-20-2009, 01:33 PM
  3. problem in socket connection in sending images
    By vibhor in forum Networking
    Replies: 2
    Last Post: 02-20-2009, 05:39 AM
  4. [SOLVED] Problem with socket programming(Vista)
    By jagatsastry in forum Networking
    Replies: 1
    Last Post: 02-02-2009, 03:56 PM
  5. Client Server socket problem - help needed
    By kellaw in forum Threads and Synchronization
    Replies: 6
    Last Post: 10-03-2008, 06:49 PM

Posting Permissions

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