首页 > 用户发贴区 > 编程问题提问区 > 求助:这段代码怎么才能在C的环境下编译通过
2008
01-01

求助:这段代码怎么才能在C的环境下编译通过

求助:这段代码能在UNIX下编译,但是无法在C下编译,哪位老师能看出是什么原因不能编译的话,帮我改一下让它能在C下编译,万分感谢!


#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <signal.h>
#include <setjmp.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/select.h>


int main(int argc,char * argv[])
{
     char SendBuf[1000],RecvBuf[1000];
     int MSGLEN;
     struct sockaddr_in   server;
     int sock;
     char msgbuf[500];
     char HandPhone[15];


    if( argc<3 )
    {
        fprintf(stdout, “usage: cmd phone msg……..\n”);
        return -1;
    }


     MSGLEN=591;
     memset(SendBuf,0,sizeof(SendBuf));
    memset(HandPhone,’\0′,sizeof(HandPhone));
    memset(msgbuf,’\0′,sizeof(msgbuf));
    strcpy(HandPhone,argv[1]);
    strcpy(msgbuf,argv[2]);
   /** printf(“Phone=%s\n”,HandPhone);
    printf(“msgbuf=%s\n”,msgbuf); **/


    if ( ( sock = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 ) {
        printf(“[创建sock出错]\n”);
       return -1;
    }



    server.sin_family = AF_INET;
    server.sin_port=htons(9899);
    server.sin_addr.s_addr=inet_addr(“182.5.5.30″);


    if ( connect( sock, (struct sockaddr *)&server, sizeof(server)) < 0 )
    {
        printf(“连接出错\n”);
     return( -1 );
     }



    memcpy(SendBuf,”18200500101820050010″,20);/*帐号  无用*/
 memset(SendBuf+20,’\0′,1);/*分隔符*/
 memcpy(SendBuf+21,”18200500101820050010″,20);/*客户号  无用*/
 memset(SendBuf+41,’\0′,1);/*分隔符*/
 memcpy(SendBuf+42,HandPhone,15);/*手机号*/
 memset(SendBuf+57,’\0′,1);/*分隔符*/
 memset(SendBuf+58,’0′,1);/*即发标志*/
 memset(SendBuf+59,’\0′,1);/*分隔符*/
 memset(SendBuf+60,’2′,1);/*收费标准*/
 memset(SendBuf+61,’\0′,1);/*分隔符*/
 if(strncmp(HandPhone,”139″,3)==0 || strncmp(HandPhone,”138″,3)==0 ||strncmp(HandPhone,”137″,3)==0 ||strncmp(HandPhone,”136″,3)==0 ||strncmp(HandPhone,”135″,3)==0 ||strncmp(HandPhone,”134″,3)==0 || strncmp(HandPhone,”159″,3)==0  )
  memcpy(SendBuf+62,”0001″,4);/*移动*/
 else if(strncmp(HandPhone,”130″,3)==0 || strncmp(HandPhone,”131″,3)==0 || strncmp(HandPhone,”132″,3)==0 || strncmp(HandPhone,”133″,3)==0)
  memcpy(SendBuf+62,”0002″,4);/*联通*/
 else
  memcpy(SendBuf+62,”0003″,4);/*小灵通*/
 memset(SendBuf+66,’\0′,1);/*分隔符*/
 
 strcpy(SendBuf+67,msgbuf);
 memset(SendBuf+567,’\0′,1);/*分隔符*/
 
 memcpy(SendBuf+568,”0000000″,7);
 memcpy(SendBuf+575,”             “,13);
 memset(SendBuf+588,’\0′,1);/*分隔符*/
 memcpy(SendBuf+589,”1″,1);
 memset(SendBuf+590,’\0′,1);/*分隔符*/


     if(send(sock,SendBuf,MSGLEN,0)<0)
     {
        printf(“发送报文错\n”);
      return( -1 );
     }
    memset(RecvBuf,’\0′,sizeof(RecvBuf));
     if(recv(sock,RecvBuf,6,0)<0)
     {
        printf(“接收报文错\n”);
      return( -1 );
     }
     printf(“返回数据:%s\n”,RecvBuf);
     close(sock);
     return 0;
}


 


 


求助:这段代码怎么才能在C的环境下编译通过》有 4 条评论

  1. xstar 说:

    你想说的是在unix下可以编译在window下不可以编译吧!

    看看这两者socket编程的差异!
    window socket需要再调用另外两个函数的,还有头文件需要更改一下!

  2. acer 说:

    对,就是这个意思。可我不知道怎么改啊,老师能帮我改一下吗?

  3. xstar 说:

    自己参考下面的代码修改一下!
    注意winsock2.h这个头文件和两个WSA开头的函数!其他的调用和unix下是一样的!
    另外注意包含window下的socket库!似乎是WS2_32!
    代码在window的MinGW环境下编译通过!
    [code]
    gcc httpproxy.c -o httpproxy.exe -std=c99 -lws2_32
    start httpproxy 192.168.52.7 80 80
    [/code]
    [code]
    #include <stdio.h>
    #include <stdlib.h>

    #include <winsock2.h>

    #define BUFFSIZ  (16 * 1024)
    #define ADDRLEN  256

    int get_listen_sockfd( int port )
    {
     struct sockaddr_in sock_addr = { 0 };
     int                sockfd    = 0;

     sock_addr.sin_family      = AF_INET;
     sock_addr.sin_port        = htons( port );
     sock_addr.sin_addr.s_addr = htonl( INADDR_ANY );

     if( ( sockfd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) {
      return -1;
     }

     if( bind( sockfd, (struct sockaddr *)&sock_addr, sizeof( sock_addr ) ) == -1 ) {
      close( sockfd );
      return -1;
     }

     if( listen( sockfd, 5 ) == -1 ) {
      close( sockfd );
      return -1;
     }

     return sockfd;
    }

    int get_connect_sockfd( char *addr, int port )
    {
     struct sockaddr_in svr_addr = { 0 };
     struct hostent     *he;
     int                sockfd   = 0;

     svr_addr.sin_addr.s_addr = inet_addr( addr );
     svr_addr.sin_port        = htons( port );
     svr_addr.sin_family      = AF_INET;

     if( ( sockfd = socket( AF_INET, SOCK_STREAM, 0 ) ) ==  -1 ){
      return -1;
     }

     if( connect( sockfd, (struct sockaddr *)&svr_addr, sizeof( svr_addr ) ) == -1 )
     {
      close( sockfd );
      return -1;
     }

     return sockfd;
    }

    void trans_data( int local_sockfd, int remote_sockfd )
    {
     int ret              = 0;

     char buff[ BUFFSIZ ] = { 0 };
     struct timeval tm    = { 0 };

     int recv_sockfd;
     int send_sockfd;
     fd_set fd_recv;
     fd_set fd_send;

     int rcvsiz               = 0;
     int sndsiz               = 0;
     int sbyte                  = 0;

     tm.tv_sec                  = 60 * 10;

     for( ; ; ){
      // recv the data
      FD_ZERO( &fd_recv );
      FD_SET( local_sockfd, &fd_recv );
      FD_SET( remote_sockfd, &fd_recv );

      recv_sockfd = (local_sockfd > remote_sockfd) ? local_sockfd : remote_sockfd;

      ret = select( recv_sockfd + 1, &fd_recv, NULL, NULL, &tm );

      if ( ret == -1 ) {  /* interrupted by signal */
       continue;
      } else if ( ret == -1 ) {                     /* real select error */
       break;
      } else if ( ret == 0 ) {                      /* timeout */
       continue;
      }

      if( FD_ISSET( local_sockfd, &fd_recv ) ) {
       recv_sockfd = local_sockfd;
       send_sockfd = remote_sockfd;

       if( ( rcvsiz = recv( recv_sockfd, buff, BUFFSIZ, 0 ) ) <= 0 ){
        break;
       }

       // send the data;
       FD_ZERO( &fd_send );
       FD_SET( send_sockfd, &fd_send );

       ret = select( send_sockfd + 1, NULL, &fd_send, NULL, &tm );

       if ( ret == -1 ) {  /* interrupted by signal */
        continue;
       } else if ( ret == -1 ) {                     /* real select error */
        break;
       } else if ( ret == 0 ) {                      /* timeout */
        continue;
       }

       if( !FD_ISSET( send_sockfd, &fd_send ) ) continue;

       sbyte = 0;

       for( ; ; ) {
        sndsiz = send( send_sockfd, &buff[ sbyte ], (rcvsiz - sbyte), 0 );

        if (sndsiz <= 0) {
         printf( "ERR!\n" );
         break;
        }

        sbyte += sndsiz;

        if (sbyte >= rcvsiz) {
         break;
        }
       } /* for( ; ; ) end */
      }

      if( FD_ISSET( remote_sockfd, &fd_recv ) ) {
       recv_sockfd = remote_sockfd;
       send_sockfd = local_sockfd;

       if( ( rcvsiz = recv( recv_sockfd, buff, BUFFSIZ, 0 ) ) <= 0 ){
        break;
       }

       // send the data;
       FD_ZERO( &fd_send );
       FD_SET( send_sockfd, &fd_send );

       ret = select( send_sockfd + 1, NULL, &fd_send, NULL, &tm );

       if ( ret == -1 ) {  /* interrupted by signal */
        continue;
       } else if ( ret == -1 ) {                     /* real select error */
        break;
       } else if ( ret == 0 ) {                      /* timeout */
        continue;
       }

       if( !FD_ISSET( send_sockfd, &fd_send ) ) continue;

       sbyte = 0;

       for( ; ; ) {
        sndsiz = send( send_sockfd, &buff[ sbyte ], (rcvsiz - sbyte), 0 );

        if (sndsiz <= 0) {
         printf( "ERR!\n" );
         break;
        }

        sbyte += sndsiz;

        if (sbyte >= rcvsiz) {
         break;
        }
       } /* for( ; ; ) end */
      }
     }
    }

    void do_work( int listen_sockfd, char *remote_ip, int remote_port )
    {
     struct sockaddr_in work_addr = {0};
     struct sockaddr_in ipaddr    = {0};

     int local_sockfd             = 0;
     int remote_sockfd            = 0;

     int addrlen                  = 0;
     int ret                      = 0;

     for( ; ; ){
      addrlen      = sizeof( struct sockaddr_in );
      if( ( local_sockfd = accept( listen_sockfd, (struct sockaddr *)&work_addr , &addrlen ) ) == -1 ) continue;

      printf( "remote computer: %s[%d]\n", remote_ip, remote_port );

      // con remote computer
      if ( ( remote_sockfd = get_connect_sockfd( remote_ip, remote_port ) ) == -1 ) {
       close( local_sockfd );
       continue;
      }

      trans_data( local_sockfd, remote_sockfd );

      close( remote_sockfd );
      close( local_sockfd );
     }
    }

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

     int listen_sockfd;
     int listen_port;

     char remote_ip[ ADDRLEN ];
     int  remote_port;

     if ( argc != 4 )
     {
      printf( "Usage : mailproxy remote_ip remote_port listen_port\n" );
      printf( "        mailproxy 218.108.31.140 25 25\n" );
      printf( "        mailproxy 218.108.31.140 110 110\n" );
      return -1;
     }

     strcpy( remote_ip, argv[ 1 ] );
     remote_port = atoi( argv[ 2 ] );
     listen_port = atoi( argv[ 3 ] );

     if ( WSAStartup(0x0202, &wsaData) != 0 ) {
      printf( "WSAStartup ERROR!\n" );
      return -1;
     }

     listen_sockfd = get_listen_sockfd( listen_port );

     if ( listen_sockfd == -1 ) {
      printf("Create listen sockfd error! port[%d]\n", listen_port);
      return 1;
     }

     printf( "listen_sockfd = %d listen_port = %d\n", listen_sockfd, listen_port );

     do_work( listen_sockfd, remote_ip, remote_port );

     close( listen_sockfd );

     WSACleanup();
    }
    [/code]

  4. acer 说:

    太好了,非常感谢

留下一个回复