tcp client in c

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

/* fpont 12/99 */
/* pont.net    */
/* tcpClient.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 SERVER_PORT 1500
#define MAX_MSG 100

int main (int argc, char *argv[]) {

  int sd, rc, i;
  struct sockaddr_in localAddr, servAddr;
  struct hostent *h;
 
  if(argc < 3) {
    printf("usage: %s <server> <data1> <data2> ... <dataN>/n",argv[0]);
    exit(1);
  }

  h = gethostbyname(argv[1]);
  if(h==NULL) {
    printf("%s: unknown host '%s'/n",argv[0],argv[1]);
    exit(1);
  }

  servAddr.sin_family = h->h_addrtype;
  memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
  servAddr.sin_port = htons(SERVER_PORT);

  /* create socket */
  sd = socket(AF_INET, SOCK_STREAM, 0);
  if(sd<0) {
    perror("cannot open socket ");
    exit(1);
  }

  /* bind any port number */
  localAddr.sin_family = AF_INET;
  localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  localAddr.sin_port = htons(0);
 
  rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr));
  if(rc<0) {
    printf("%s: cannot bind port TCP %u/n",argv[0],SERVER_PORT);
    perror("error ");
    exit(1);
  }
    
  /* connect to server */
  rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
  if(rc<0) {
    perror("cannot connect ");
    exit(1);
  }

  for(i=2;i<argc;i++) {
   
    rc = send(sd, argv[i], strlen(argv[i]) + 1, 0);
   
    if(rc<0) {
      perror("cannot send data ");
      close(sd);
      exit(1);
   
    }

    printf("%s: data%u sent (%s)/n",argv[0],i-1,argv[i]);

  
  }

return 0;
 
}

Non-Blocking Sockets in TCP/IP (The Client)

Winsock Tutorial 3 Non-Blocking Sockets in TCP/IP (The Client) This tutorial is very similar to ...

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
  • 808

c 阻塞 循环 tcp client

  • 2015年08月13日 19:39
  • 2KB
  • 下载

tcp_client_send_bind8888.c

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

tcp_client.c 客户程序服务模型

#include #include #include #include #include #include #include #include #define port...

Simple TCP Server Client Socket C

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

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
  • 下载

回射客户端服务器中僵尸进程的处理( the solution of zombie process in the echo client && server )

Tips : In the passage , we discuss how to clean up zombie processes with a better solution. We atte...

Simple HTTP Server and Client in Python

This time I'd like to show you how to make a simple HTTP server and client in python. It's a bit dif...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:tcp client in c
举报原因:
原因补充:

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