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

Thread: How to Create a server socket to listen for incoming connections?

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Post How to Create a server socket to listen for incoming connections?

    With this code, we can create a Server Socket in Java to listen for incoming connections.

    This code is made up of 2 classes, PortMonitor & OneConnection.

    PortMonitor will open a desired port and watch for the incoming connection. Once a connection is established, the OneConnection class will relay back any incoming data.

    Once this code is compiled, you can test it by using telnet to connect locally to the desired port.

    import java.net.*;
    import java.io.*;
     
    public class PortMonitor {
     
        /**
         * JavaProgrammingForums.com
         */
        public static void main(String[] args) throws Exception {
     
            //Port to monitor
            final int myPort = 101;
            ServerSocket ssock = new ServerSocket(myPort);
            System.out.println("port " + myPort + " opened");
     
            Socket sock = ssock.accept();
            System.out.println("Someone has made socket connection");
     
            OneConnection client = new OneConnection(sock);
            String s = client.getRequest();
     
        }
     
    }
     
    class OneConnection {
        Socket sock;
        BufferedReader in = null;
        DataOutputStream out = null;
     
        OneConnection(Socket sock) throws Exception {
            this.sock = sock;
            in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            out = new DataOutputStream(sock.getOutputStream());
        }
     
        String getRequest() throws Exception {
            String s = null;
            while ((s = in.readLine()) != null) {
                System.out.println("got: " + s);
            }
            return s;
        }
    }

    Example output:

    port 101 opened
    Someone has made socket connection
    got: test
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to Create a server socket to listen for incoming connections

    The port is opening but it's not saying anything about a connection.

    However, I'm not using telenet as I've kinda forgotten what that was.

    Is it like a network of computers (often used in a business or school)?

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: How to Create a server socket to listen for incoming connections

    Telnet - Wikipedia, the free encyclopedia

    In Windows, click Start > Run > CMD (for a command window) Type telnet
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. The Following User Says Thank You to JavaPF For This Useful Post:

    daniel.j2ee (October 28th, 2011)

  5. #4
    Member
    Join Date
    Oct 2011
    Posts
    50
    My Mood
    Fine
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: How to Create a server socket to listen for incoming connections

    Right what I needed. Thank you.

    PS: I have done some modifications to this: I mean, I keep track of all the connections into an array of "oneConnections", and made the class to extend Thread, so every client will have it's own thread.

    application context
    Last edited by daniel.j2ee; December 13th, 2011 at 04:59 PM.

Similar Threads

  1. Java error "could not create java virtual machine"
    By aubrey4444 in forum Java Theory & Questions
    Replies: 17
    Last Post: October 3rd, 2010, 12:51 PM
  2. How can i create fake IP addresses to extract information for the DB?
    By neomancer in forum Java Theory & Questions
    Replies: 4
    Last Post: May 8th, 2009, 04:54 AM
  3. Replies: 1
    Last Post: May 7th, 2009, 04:31 AM
  4. Replies: 2
    Last Post: October 7th, 2008, 11:03 PM