tcp server in c

原创 2005年05月26日 15:43:00

/* fpont 1/00 */
/* pont.net    */
/* tcpServer.c */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h> /* close */


#define SUCCESS 0
#define ERROR   1

#define END_LINE 0x0
#define SERVER_PORT 1500
#define MAX_MSG 100

/* function readline */
int read_line();

int main (int argc, char *argv[]) {
 
  int sd, newSd, cliLen;

  struct sockaddr_in cliAddr, servAddr;
  char line[MAX_MSG];


  /* create socket */
  sd = socket(AF_INET, SOCK_STREAM, 0);
   if(sd<0) {
    perror("cannot open socket ");
    return ERROR;
  }
 
  /* bind server port */
  servAddr.sin_family = AF_INET;
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servAddr.sin_port = htons(SERVER_PORT);
 
  if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0) {
    perror("cannot bind port ");
    return ERROR;
  }

  listen(sd,5);
 
  while(1) {

    printf("%s: waiting for data on port TCP %u/n",argv[0],SERVER_PORT);

    cliLen = sizeof(cliAddr);
    newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen);
    if(newSd<0) {
      perror("cannot accept connection ");
      return ERROR;
    }
   
    /* init line */
    memset(line,0x0,MAX_MSG);
   
    /* receive segments */
    while(read_line(newSd,line)!=ERROR) {
     
      printf("%s: received from %s:TCP%d : %s/n", argv[0],
      inet_ntoa(cliAddr.sin_addr),
      ntohs(cliAddr.sin_port), line);
      /* init line */
      memset(line,0x0,MAX_MSG);
     
    } /* while(read_line) */
   
  } /* while (1) */

}


/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING       */
/* this function is experimental.. I don't know yet if it works  */
/* correctly or not. Use Steven's readline() function to have    */
/* something robust.                                             */
/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING       */

/* rcv_line is my function readline(). Data is read from the socket when */
/* needed, but not byte after bytes. All the received data is read.      */
/* This means only one call to recv(), instead of one call for           */
/* each received byte.                                                   */
/* You can set END_CHAR to whatever means endofline for you. (0x0A is /n)*/
/* read_lin returns the number of bytes returned in line_to_return       */
int read_line(int newSd, char *line_to_return) {
 
  static int rcv_ptr=0;
  static char rcv_msg[MAX_MSG];
  static int n;
  int offset;

  offset=0;

  while(1) {
    if(rcv_ptr==0) {
      /* read data from socket */
      memset(rcv_msg,0x0,MAX_MSG); /* init buffer */
      n = recv(newSd, rcv_msg, MAX_MSG, 0); /* wait for data */
      if (n<0) {
 perror(" cannot receive data ");
 return ERROR;
      } else if (n==0) {
 printf(" connection closed by client/n");
 close(newSd);
 return ERROR;
      }
    }
 
    /* if new data read on socket */
    /* OR */
    /* if another line is still in buffer */

    /* copy line into 'line_to_return' */
    while(*(rcv_msg+rcv_ptr)!=END_LINE && rcv_ptr<n) {
      memcpy(line_to_return+offset,rcv_msg+rcv_ptr,1);
      offset++;
      rcv_ptr++;
    }
   
    /* end of line + end of buffer => return line */
    if(rcv_ptr==n-1) {
      /* set last byte to END_LINE */
      *(line_to_return+offset)=END_LINE;
      rcv_ptr=0;
      return ++offset;
    }
   
    /* end of line but still some data in buffer => return line */
    if(rcv_ptr <n-1) {
      /* set last byte to END_LINE */
      *(line_to_return+offset)=END_LINE;
      rcv_ptr++;
      return ++offset;
    }

    /* end of buffer but line is not ended => */
    /*  wait for more data to arrive on socket */
    if(rcv_ptr == n) {
      rcv_ptr = 0;
    }
   
  } /* while */
}
 
 

Remote Connection via Proxy Server in TCP/IP Socket

说一下我想写这个干嘛: 就是能够连接在NAT转发环境下无DNS解析的主机。 由于NAT转发下的主机没有办法通过外网ip直连,所以代理模式必须使用主动模式。 我简单说一下大概思路:Created ...
  • mpsk07
  • mpsk07
  • 2016年10月02日 18:16
  • 149

linux c tcp server

  • 2012年11月29日 14:09
  • 3KB
  • 下载

UDP Server Client Implementation in C for Unix/Linux

Here's a simple UDP Server Client Implementation in C for Unix/Linux. As UDP is a connection-less pr...
  • no_mame
  • no_mame
  • 2013年12月08日 09:20
  • 812

TCP/IP Sockets in C

  • 2008年06月26日 08:57
  • 6.36MB
  • 下载

TCP/IP Sockets in C 2rd Edition

  • 2009年08月07日 23:12
  • 1.28MB
  • 下载

Server(Admin) start is failing in WLST as outofmemory error permgen space for weblogic 12c

OutOfMemory Causes and First Aid Steps? In response to the Comment: http://middlewaremagic.co...
  • lzy_168
  • lzy_168
  • 2012年07月25日 14:09
  • 2895

tcp/ip 程序 (改进版) server.c

/*************************************************** 版权说明:该程序为Shaw Song所有 版本号: 2.0 生成日期:2016.8.2 作者...

tcp_server_localhost.c

#include #include #include #include #include #include #include #include #define por...

Simple TCP Server Client Socket C

转载链接:http://blog.163.com/caipeipei_love@126/blog/static/2596603220101118433940/ tcpserver.c #i...

tcp_server.c服务模型

#include #include #include #include #include #include #include #include #define po...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:tcp server in c
举报原因:
原因补充:

(最多只允许输入30个字)