Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Error with C code

Avatar of tooki
tooki asked on
Unix OSNetworkingEditors IDEs
6 Comments1 Solution369 ViewsLast Modified:
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_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");
===============

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");
	
....
.....

....
}
ASKER CERTIFIED SOLUTION
Avatar of arnold
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answers