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
ServerThread.java
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.
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: