Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

c server

c server

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");
     val = 1;
       /*initialize ports and address*/
     if (setsockopt(soc, SOL_SOCKET, SO_REUSEADDR, &val, sizeof val) != 0)
    { perror("server:setsockopt");
       /*bind the socket(assign a port number */
     if (bind(soc, (struct sockaddr *)&self, sizeof(self)) == -1)
       {  perror("server:bind");
       /*make it listening */
       listen(soc, 30);
     /* accept connection request */
     ns = accept(soc, (struct sockaddr *)&client_addr, &addrlen);       
     if (ns < 0)
       {  perror("server:accept");
     /* 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);
/***** end of inetserver.c *****/

Thank You Guys!!!

RE: c server

What error are you getting? Did you check network connectivity?


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close