I’m a Java programmer, but I’m starting to learn C programming language. Hope someone can help me here…
My server side socket is coded using Java, and the client side is coded through C. I can telnet to the Java app and the Java app shows that the input is being received from telnet.
However, when I use my C program to connect to the Java app, it shows that they are connected, but the Java app couldn’t receive any input from the C program.
Someone told me that I need to check if the way of the C Program is sending the data is “compatible” with my Java app, but since i’m new to C, I hope someone could do a quick go through my C source below.
My Java app receives input character by character, as shown in below code:
..
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);
}
However, on my client side, it’s a C program, and sending through:
Part of main.cpp
//-----------------------------------------------------------------//
// Function: SendMessage()
// Input Params:
// *Message : string with the message to send.
// Output Params:
// Description:
// Sends the message using TCP or UDP socket.
//-----------------------------------------------------------------//
VOID SendMessage( CHAR *Message )
{
INT ReturnCode;
#ifdef TCP
ReturnCode = TCP_SendData((CHAR *)IPaddr, dest_port, Message, strlen(Message));
if(RemoteServerDisconnected || (ReturnCode == -1)) {
Status = STOP_INET_STATE;
}else {
WriteTraces("TCP-> All data are sent");
}
#else
ReturnCode = UDP_SendData((CHAR *) IPaddr, dest_port, Message, strlen( Message));
if(ReturnCode == -1) {
ConnectionError = TRUE;
} else {
WriteTraces("UDP-> All data are sent");
}
#endif
}
Part of socket.cpp
//-----------------------------------------------------------------//
// Function: TCP_SendData()
// Input Params:
// Output Params:
// Description:
//-----------------------------------------------------------------//
INT TCP_SendData( CHAR *ip, INT port, CHAR *buffer, INT buffer_length )
{
struct sockaddr_in server;
struct hostent *hp;
INT SocketFD;
struct timeval timeout;
INT error;
INT sent_bytes = 0;
INT total_bytes = 0;
INT tries;
WriteTraces("
SOCKET->TCP: TCP_SendData() start
");
//Time outs on socket operations
timeout.tv_sec = 5;
timeout.tv_usec = 0;
// sigpipe_signal.sa_handler = sigpipe_handler;
// sigpipe_signal.sa_flags = 0;
// sigaction(SIGPIPE, &sigpipe_signal, &sigpipe_signalOld);
// Obtain host data
if ((hp = gethostbyname ((CHAR*)ip)) == 0) {
perror("gethostbyname() failed");
return -1;
}
else { // We can resolve the host so we continue
memset((CHAR*)&server, 0, sizeof(server));
// Construct the server address structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
server.sin_port = htons(port);
//Obtain socket
if ((SocketFD = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket() failed");
return -1;
}
/*
//Set time-out parameters
if (setsockopt(SocketFD, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) == -1) {
perror("setsockopt() failed");
close(SocketFD);
return -1;
}
if (setsockopt(SocketFD, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) == -1) {
perror("setsockopt() failed");
close(SocketFD);
return -1;
}
*/
// Set non-blocking for the socket
if (fcntl(SocketFD, F_SETFL, O_NONBLOCK)) {
printf("SOCKET->TCP Error making socket file descriptor non blocking
");
close(SocketFD);
return -1;
}
WriteTraces("SOCKET->TCP: connecting to the server");
// Conect with specified port of the server.
// If the connection cannot be established immediately and O_NONBLOCK is set
// for the file descriptor for the socket, connect() shall fail and set errno
// to [EINPROGRESS], but the connection request shall not be aborted.
// Subsequent calls to connect() for the same socket, before the connection is
// established, shall fail and set errno to [EALREADY].
do {
error = connect(SocketFD, (struct sockaddr*)&server, sizeof(server));
if ((error == -1) && (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EAGAIN)) {
// printf("connect:%s %d
", strerror(errno), errno);
perror("connect() failed");
close(SocketFD);
return -1;
}
}while(error != 0);
/*
// Uncomment the following lines in case of Blocking socket.
// Connect with specified port of the server
if (connect(SocketFD, (struct sockaddr*)&server, sizeof(server)) == -1) {
printf("connect: %s %d
", strerror(errno), errno);
close(SocketFD);
perror("connect() failed");
return -1;
}
*/
WriteTraces("SOCKET->TCP: socket connected");
tcp_fd = SocketFD;
TCP_StartRXThread();
(*Fncusecsleep)(1, 0);
//Send message to the server
do {
WriteTraces("
SOCKET->TCP: sending %s, %d bytes
", buffer, buffer_length);
sent_bytes = sendto(SocketFD, buffer + total_bytes, buffer_length - total_bytes, 0,
(struct sockaddr *)&server, sizeof(server));
if (sent_bytes == -1) {
TCP_StopRXThread();
close(SocketFD);
return -1;
}
total_bytes += sent_bytes;
} while(total_bytes < buffer_length);
WriteTraces("SOCKET->TCP: send message OK");
//Get an answer from the server
tries = 0;
do {
pthread_mutex_lock(&RxLock);
if (!rx_data_received && (tries != 1)) {
pthread_mutex_unlock(&RxLock);
(*Fncusecsleep)(1, 0);
}
else {
if(rx_data_received) {
pthread_mutex_unlock(&RxLock);
break;
}
else
pthread_mutex_unlock(&RxLock);
}
}while(tries++ < 120); // If no answer from the server wait for 2 minutes
TCP_StopRXThread();
close(SocketFD);
}
// remoteHostDisc = FALSE;
WriteTraces("SOCKET->TCP: TCP_SendData() end");
return 0;
}
Any idea what’s the problem?