tooki
asked on
Error with C code
I have this C code that compiles ok on the server. However, when run it gives the error:
A Listen socket has been opened in the server!
The bind connection in the server could not be made!
The error is coming from the below few lines of the code:
========
setsockopt(listenfd,SOL_SO CKET,SO_SN DBUF,(char *)&tcp_buf_size,(int)sizeo f(tcp_buf_ size));
setsockopt(listenfd,SOL_SO CKET,SO_RC VBUF,(char *)&tcp_buf_size,(int)sizeo f(tcp_buf_ size));
setsockopt(listenfd,SOL_SO CKET,SO_RE USEADDR,(c har *)&one, sizeof(one));
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(CT_HOST);
serv_addr.sin_port = htons(PORT);
if (bind (listenfd, (struct sockaddr *)&serv_addr,sizeof(serv_a ddr)) < 0)
print_error("The bind connection in the server could not be made!");
else
printf("The bind connection in the server has been made!\n");
===============
The same code (with different CT_HOST) compiles and runs without the other error on a difference server. I do not know how to choose the port number (here I put 9031). Is that a possible problem?
A Listen socket has been opened in the server!
The bind connection in the server could not be made!
The error is coming from the below few lines of the code:
========
setsockopt(listenfd,SOL_SO
setsockopt(listenfd,SOL_SO
setsockopt(listenfd,SOL_SO
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(CT_HOST);
serv_addr.sin_port = htons(PORT);
if (bind (listenfd, (struct sockaddr *)&serv_addr,sizeof(serv_a
print_error("The bind connection in the server could not be made!");
else
printf("The bind connection in the server has been made!\n");
===============
The same code (with different CT_HOST) compiles and runs without the other error on a difference server. I do not know how to choose the port number (here I put 9031). Is that a possible problem?
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <sys/times.h>
#include <string.h>
#include <errno.h>
extern int errno; /* Unix error number */
#include <libgen.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dlfcn.h>
#include <strings.h>
#include <netinet/tcp.h>
#ifndef HTTP_TOOLS_H
#define HTTP_TOOLS_H
#endif /* HTTP_TOOLS_H */
#define TRUE 1
#define FALSE 0
#define CT_HOST "xx.xxx.xxx.xxx"
#define PORT 9031
#define BUFFER_SIZE BLOCK_SIZE+4
#define BLOCK_SIZE 4096
int main(int argc, char *argv[])
{
int listenfd, connfd;
struct sockaddr_in serv_addr, cli_addr;
int tcp_buf_size=1024*256;
int one=1;
pid_t childpid;
char addr_str[50];
socklen_t cli_len;
/* If any arguments supplied, quit. */
if (argc > 1) exit(1);
/* Create a TCP Socket */
if ( (listenfd = socket(AF_INET,SOCK_STREAM,0)) < 0 )
print_error("A Listen socket could not be opened in the server!\n");
else
printf("A Listen socket has been opened in the server!\n");
setsockopt(listenfd,SOL_SOCKET,SO_SNDBUF,(char *)&tcp_buf_size,(int)sizeof(tcp_buf_size));
setsockopt(listenfd,SOL_SOCKET,SO_RCVBUF,(char *)&tcp_buf_size,(int)sizeof(tcp_buf_size));
setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,(char *)&one, sizeof(one));
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(CT_HOST);
serv_addr.sin_port = htons(PORT);
if (bind (listenfd, (struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
print_error("The bind connection in the server could not be made!");
else
printf("The bind connection in the server has been made!\n");
....
.....
....
}
test test
depending on which unix/linux operating system you are on, you can use strace /path/to/server_applicatio n or truss -f /path/to/server_applicatio n and then watch the output on what is going on.
You may have iptables/selinux,portmappe r , etc. that is preventing your application from binging to this part. You would need to define within portmapper an exception for this 9031 port.
You may have iptables/selinux,portmappe
ASKER
Thanks.It is UNIX Solaris 10 operating system.
The port number 9031 I chose because it was the working code on the other server. So the port number 9031 is a arbitrary port number for the current server on which I am getting the bind error.
How do I find a port number that could be appropriate for this Solaris server?
The port number 9031 I chose because it was the working code on the other server. So the port number 9031 is a arbitrary port number for the current server on which I am getting the bind error.
How do I find a port number that could be appropriate for this Solaris server?
try running lsof -i:9031 to see if you have anything currently bound to it.
truff -f is how you can see what running your server application does and which resources as well as what the error code (errno) when the bind is attempted and fails.
Is the user with whose credentials you are starting this server_application has root access or are you starting it with a regular user?
truff -f is how you can see what running your server application does and which resources as well as what the error code (errno) when the bind is attempted and fails.
Is the user with whose credentials you are starting this server_application has root access or are you starting it with a regular user?
ASKER
No I am running this as "root" on the server. So there is no permission issue.
#truff -f
truff: not found
#lsof -i:9031
There was a huge text file got created /.lsof_v02216 .
Nothing is currently bound right now.
If I do ps -ef command I see that the program is not running.
ps -ef on the other server (on which the programs run ok) returns the program name.
#truff -f
truff: not found
#lsof -i:9031
There was a huge text file got created /.lsof_v02216 .
Nothing is currently bound right now.
If I do ps -ef command I see that the program is not running.
ps -ef on the other server (on which the programs run ok) returns the program name.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.