Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c server

Status
Not open for further replies.

lemaire

Programmer
Mar 12, 2009
1
US
Greetings,
I have a client written in php that is trying to connect to the following c server. Unfortunately, i am getting a warning message and the connection is refused. Since this is a c forum, i have attached the c code and would highly appreciate if someone could tell me if there is anything wrong with the code:

/***** inetserver.c *****/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> /* for getenv */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> /* Internet domain header */
#include <resolv.h>
#include <arpa/inet.h>
#include <errno.h>


#define SERVER_PORT 10022
struct sockaddr_in self = {AF_INET, 0};

int main()
{ int soc, ns, k;
char buf[2048];
struct sockaddr_in client_addr = {AF_INET};
self.sin_port = htons(SERVER_PORT);
socklen_t addrlen = sizeof(client_addr);

int val;
/* set up listening socket soc */
soc = socket(AF_INET, SOCK_STREAM, SOL_TCP);
if (soc < 0)
{ perror("server");
exit(1);
}
val = 1;

/*initialize ports and address*/
if (setsockopt(soc, SOL_SOCKET, SO_REUSEADDR, &val, sizeof val) != 0)
{ perror("server:setsockopt");
exit(1);
}
/*bind the socket(assign a port number */
if (bind(soc, (struct sockaddr *)&self, sizeof(self)) == -1)
{ perror("server:bind");
close(soc);
exit(1);
}

/*make it listening */
listen(soc, 30);

/* accept connection request */
ns = accept(soc, (struct sockaddr *)&client_addr, &addrlen);
if (ns < 0)
{ perror("server:accept");
close(soc);
exit(1);
}
/* data transfer on connected socket ns */
k = read(ns, buf, sizeof(buf) - 1);
buf[k] = '\0'; /* null-terminate string */
printf("SERVER RECEIVED: %s\n",buf);
write(ns, buf, k);
close(ns); close(soc);
return(0);
}
/***** end of inetserver.c *****/

Thank You Guys!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top