Connecting to a java socket - server

Discussion in 'Java' started by tuckker, Feb 21, 2009.

  1. tuckker

    tuckker

    Joined:
    Feb 21, 2009
    Messages:
    1
    I have a client program written using C, to send data to a Server for processing.

    However I'm trying to write the Java on the server side to receive the data sent from the client. The server side Java prog shows that the client has connected with the port and IP, however it won't show what data has been sent.

    My Java code is as following:

    Main.java
    Code:
    import java.net.*;
    import java.io.*;
    
    /**
     *
    
     */
    public class Main {
    
        public final static int defaultPort = 2000;
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    
            int port = defaultPort;
    
            try {
                port = Integer.parseInt(args[0]);
            } catch (Exception e) {
            }
            if (port <= 0 || port >= 65536) {
                port = defaultPort;
            }
    
            try {
                ServerSocket ss = new ServerSocket(port);
                
                while (true) {
                    try {
                        System.out.println("Listening on port "+port+" has started...");
                        Socket s = ss.accept();
                        (new Thread(new ServerThread(s))).start();
                        
                        
                    } catch (IOException e) {
                    }
                }
            } catch (IOException e) {
                System.err.println(e);
            }
    
        }
    }
    ServerThread.java
    Code:
    import java.net.*;
    import java.io.*;
    
    /**
     *
     */
    public class ServerThread extends Thread {
    
        private Socket socket = null;
    
        public ServerThread(Socket socket) {
            super("ServerThread");
            this.socket = socket;
        }
    
        public void run() {
    
            try {
                if (socket.isConnected()) {
                    System.out.println("Server is connected to IP: " + socket.getInetAddress() + " Port: " + socket.getPort() + "");
                }
    
                // Get data from remote device
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(
                        socket.getInputStream()));
    
                
                try {
                    while (true) {
                        int i = in.read();
                        if (i == -1) {
                            break;
                        }
                        char c = (char) i;
                        System.out.print(c);
                    }
                    
                } catch (IOException e) {
                    System.err.println(e);
                }
     
    
    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    But if I telnet to the server, and type in the command prompt, it shows that the text is being registered on the server side Java prog.

    What could be wrong?

    P.s: I'm positive that the C program on the client side is coded properly.
     
    Last edited: Feb 21, 2009
    tuckker, Feb 21, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,858
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    974
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    553
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    669
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    722
Loading...

Share This Page