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 8 of 8

Thread: Socket Programming Problem

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Socket Programming Problem

    hi all,
    i am trying o study socket programming in java and trying to create a simple chat program, i found this code on a tutorial on web and tried playing with it, the problem is it seems it cant connect to the server.

    this is my code for the server side:

    import java.io.*;
    import java.net.*;
     
     
    public class ServerSide{
        public static void main(String[] args)throws Exception {
            BufferedReader In = new BufferedReader(new InputStreamReader(System.in));
            String data = In.readLine();
     
             ServerSocket srvr = new ServerSocket(8000);
             Socket skt = srvr.accept();
             System.out.print("Server has connected!\n");
             PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
             System.out.print("Sending string: '" + data + "'\n");
             out.print(data);
             out.close();
             skt.close();
             srvr.close();
     
        }
    }

    and this is my code for the client side:

    import java.io.*;
    import java.net.*;
     
     
    public class ClientSide{
        public static void main(String[] args)throws Exception {
             Socket skt = new Socket("192.168.5.236", 8000);
             BufferedReader in = new BufferedReader(new
                InputStreamReader(skt.getInputStream()));
             System.out.print("Received string: '");
     
             while (!in.ready()) {}
             System.out.println(in.readLine()); // Read one line and output it
     
             System.out.print("'\n");
             in.close();
        }
    }


    this is the error when i try to run the client side.

    D:\Sockets Test>java ClientSide
    Exception in thread "main" java.net.ConnectException: Connection refused: connec
    t
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at ClientSide.main(ClientSide.java:8)


    anyone can help me where i am wrong on this?. thanks.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,103
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Socket Programming Problem

    Are your client and server both on localhost, or different machines?

  3. #3
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: Socket Programming Problem

    Quote Originally Posted by copeg View Post
    Are your client and server both on localhost, or different machines?
    for now im trying to run it on localhost, then if i get it to work im gonna run it on a remote machine, 192.168.5.236 is the localhost's IP, i tried changing that lo "localhost" still doesnt work.

  4. #4
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Socket Programming Problem

    You need to port-forward the port you're using for the server.

  5. #5
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: Socket Programming Problem

    hmm.. do i have to forward the port even im just running it on local machine?

  6. #6
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Socket Programming Problem

    No. use "localhost" as the IP. you were using the ip "192.168.5.236" Which is your network IP giving to you by your router. network/private and public IPs need a portforward.

  7. #7
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: Socket Programming Problem

    i already got it.. hehehehehe... thanks anyways..

  8. #8
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Socket Programming Problem

    What did you change?

Similar Threads

  1. problem with closing connection to client socket
    By sunitha in forum Java Networking
    Replies: 1
    Last Post: December 11th, 2010, 03:28 AM
  2. Partial messages problem with Java non-blocking socket communication
    By perl0101 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 28th, 2010, 03:38 PM
  3. Java mailer programming code problem
    By shadeslayer88 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 1st, 2010, 01:21 AM
  4. Java socket programming
    By lucy in forum Java Networking
    Replies: 1
    Last Post: April 26th, 2010, 09:13 PM
  5. Newbie Programming problem
    By Pingu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2010, 01:10 AM