Connecting to a java socket - server

Joined
Feb 21, 2009
Messages
1
Reaction score
0
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:

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top