Chinaunix首页 | 论坛 | 博客
  • 博客访问: 542022
  • 博文数量: 150
  • 博客积分: 5010
  • 博客等级: 大校
  • 技术积分: 1861
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-17 00:19
文章分类

全部博文(150)

文章存档

2011年(1)

2009年(14)

2008年(135)

我的朋友

分类: LINUX

2008-09-19 16:53:40

重要参考:

如连校内: ./client 80  ""

#include<stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define SERVPORT 3333
#define MAXDATASIZE 1000 /*每次最大数据传输量 */

typedef unsigned char UINT8;
typedef signed char INT8;
typedef unsigned short int UINT16;
typedef signed short int INT16;
typedef unsigned int UINT32;
typedef signed int INT32;
typedef unsigned char BOOLEAN;
typedef unsigned short int TC_OPCODE_T;
typedef struct
{
    UINT8 reserved1 : 7; // Reserved

    UINT8 cmd_rsp_flag : 1; // Command/Response Flag 0 indicate command 1 response

    UINT8 seq_tag; // Sequence Tag // ensure the same command between request and response

    TC_OPCODE_T opcode; // Opcode

    UINT8 client_tag; // Reserved

    UINT8 no_rsp_reqd_flag : 1; // No Response Required Flag 0 expected response 1 no expected

    UINT8 reserved3 : 7; // Reserved

    UINT16 reserved4; // Reserved

    UINT32 length; // Data Length of Request

} TC_CMD_REQ_HDR_T;

typedef struct tc_tcmd
{
    TC_CMD_REQ_HDR_T cmd_header; // TCMD command header

    //if will change the communication mode between AP<-->CP or AP<-->CP???

    int cmd_sender;
    void * cmd_pdata; // TCMD cmd data

} TC_TCMD_T;


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

      /*
          TC_CMD_REQ_HDR_T header;
          TC_CMD_REQ_HDR_T header1;
          TC_TCMD_T tcmd;
          TC_TCMD_T tcmd_one;
           TC_TCMD_T tcmd_two;
          TC_TCMD_T *ptcmd=&tcmd_two;
          int counter;
          char buffer[]="hello";
          char buffer_test[20];

          header.opcode=0x0039;
          header.length=0;
          tcmd.cmd_header=header;
          tcmd.cmd_pdata=NULL;

          header1.opcode=0x0039;
          header1.length=strlen(buffer);
          tcmd_one.cmd_header=header1;
     tcmd_one.cmd_pdata=buffer;
           printf( " tcmd_header length is %d\n ",sizeof(TC_CMD_REQ_HDR_T) );
          printf( " tcmd length is %d\n ",sizeof(tcmd) );
          printf( " tcmd_one length is %d\n ",sizeof(tcmd_one) );
          memcpy( buffer_test, &tcmd_one , sizeof(TC_CMD_REQ_HDR_T));
          memcpy( buffer_test+sizeof( TC_CMD_REQ_HDR_T),"hello" , 6 );
          memcpy( &(tcmd_two.cmd_header), buffer_test, sizeof(TC_CMD_REQ_HDR_T));
          //memcpy( &(tcmd_two.cmd_header), buffer_test, sizeof(TC_CMD_REQ_HDR_T));
         
          tcmd_two.cmd_pdata= buffer_test+ sizeof(TC_CMD_REQ_HDR_T);
          printf( "data: %s\n" ,ptcmd->cmd_pdata);
*/

//socket

    int sockfd, recvbytes;
    char buf[MAXDATASIZE];
    struct hostent *host;
    struct sockaddr_in serv_addr;
    
    char send_buf[200];
    snprintf(send_buf, 200 - 1,"GET /%s HTTP/1.1\r\nHOST:%s\r\nCONNECTION:CLOSE\r\n\r\n",argv[3], argv[1]);
    printf("send_buf:%s" , send_buf );
    if (argc < 3) {
       fprintf(stderr,"Please enter the server's hostname and port!\n");
       exit(1);
    }

    if((host=gethostbyname(argv[1]))==NULL) {
      perror("gethostbyname出错!");
      exit(1);
    }

/*
 if(inet_aton(argv[1],&serv_addr.sin_addr)!=0)
                {
                   host=gethostbyaddr((char *)&serv_addr.sin_addr,4,AF_INET);
                   printf("Address information of Ip %s\n",argv[1]);
                }
 */

   if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
      perror("socket创建出错!");
      exit(1);
   }
   serv_addr.sin_family=AF_INET;
   serv_addr.sin_port=htons(atoi(argv[2]));
   printf( "port:%d\n", atoi(argv[2]));
   serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
   //inet_aton(argv[1],&serv_addr.sin_addr);

  //serv_addr.sin_addr.s_addr = INADDR_ANY;

   printf("argv[1]%s\n" , argv[1]);
   printf( "inet_aton success!\n" );
   bzero( &(serv_addr.sin_zero),8);
   printf( "receiving....!\n" );
   if (connect(sockfd, (struct sockaddr *) &serv_addr, \
      sizeof(struct sockaddr)) == -1) {
      perror("connect出错!");
      exit(1);
   }
   printf( "receiving..hostname..%s!\n",host->h_name );
   printf( " serv_addrlength is %d ",sizeof(serv_addr) );
  if (send(sockfd, (void *)send_buf, 200 , 0) == -1){
       perror( "send出错!");
       close(sockfd);
       exit(0);
      }
 
    printf( "received...\n" );
while(1)
   {
     if ((recvbytes=recv(sockfd, buf, MAXDATASIZE, 0)) ==-1) {
      
      perror("recv出错!");
      exit(1);
     }
     if( recvbytes==0 ){break;};
       buf[recvbytes] = '\0';
       printf( "Received: %s",buf);
       break;
   }
   buf[recvbytes] = '\0';
   printf( "Received: %s",buf);
   close(sockfd);
}


服务端:

#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 3333 /*服务器监听端口号 */
#define BACKLOG 10 /* 最大同时连接请求数 */
int main()
{
  int sockfd,client_fd; /*sock_fd:监听socket;client_fd:数据传输socket */
  int  sin_size;
  struct sockaddr_in my_addr; /* 本机地址信息 */
  struct sockaddr_in remote_addr; /* 客户端地址信息 */
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
     perror( "socket创建出错!"); exit(1);
  }
  my_addr.sin_family=AF_INET;
  my_addr.sin_port=htons(SERVPORT);
  my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  bzero( &(my_addr.sin_zero),8);

  if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1) {
   perror( "bind出错!");
   exit(1);
  }
  if (listen(sockfd, BACKLOG) == -1) {
    perror( "listen出错!");
   exit(1);
  }
//printf( "server address %s\n", inet_ntoa(my_addr.sin_addr));
  while(1) {
    sin_size = sizeof(struct sockaddr_in);
    printf( "begin accept:\n" );
    if ((client_fd = accept(sockfd, (struct sockaddr *) &remote_addr, &sin_size)) == -1) {
       perror( "accept出错");
       continue;
    }
    printf( "end accept:" );
    printf( "received a connection from %s\n", inet_ntoa(remote_addr.sin_addr));
    if (!fork()) { /* 子进程代码段 */
      if (send(client_fd, "Hello, you are connected!\n", 26, 0) == -1){
       perror( "send出错!");
       close(client_fd);
       exit(0);
      }
     close(client_fd);
    }
  }
}

阅读(1428) | 评论(0) | 转发(0) |
0

上一篇:segment fault

下一篇:能用的socket 客户端

给主人留下些什么吧!~~