| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 886 人关注过本帖
标题:大家看下这个程序哪里出问题了
只看楼主 加入收藏
Waiting_alone
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2008-10-28
收藏
 问题点数:0 回复次数:0 
大家看下这个程序哪里出问题了
是一个关于Socket的收发邮件程序
#include   <winsock2.h>   
  #include   <stdio.h>   
  #include   <stdlib.h>   
  #include   <windows.h>   
  #pragma   comment   (lib,"ws2_32.lib")//引入的库文件   
  
  char  server[40],to[40],from[40],subject[40],data[1024],szSend[7][2048];   
  int   times;   
  struct   sockaddr_in   sa;   
  void   usage(char   *name)
    {   
        printf("\n%s   by   ------>"    "\n2008/10/29"    "\nUsage:%s   <ServerIP>   <To>   <From>   <Subject>   <Data>   <times>"    "\nExample:%s   smtp.   AAA@   BBB@   Hello   heihei   100\n",    name,name,name);   
    return    ;

    }   
  DWORD   WINAPI   SendMail(PVOID   p)   
  {   
    WSADATA   wsd;   
    int   net,ret,i,j;   
    char  szBuffer[2048];   
    if(WSAStartup(MAKEWORD(1,1),&wsd)!=0)//调用Windows Sockets DLL   
    {   
            printf("\nFailed        to        load        Winsock        library!ErrorCode:%d.",GetLastError());   
            ExitProcess(1);//退出线程   
    }   
        net=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);   
    if(net==INVALID_SOCKET)   
    {   
            printf("\nsocket()   failed.ErrorCode:%d.",GetLastError());   
            WSACleanup();//中止Windows Sockets DLL的使用   
            ExitProcess(1);   
    }   
        ret=connect(net,   (struct   sockaddr   *)   &sa,   sizeof   sa);   
    if(ret!=0)   
    {   
            printf("\nconnect()   failed.ErrorCode:%d.",GetLastError());   
            closesocket(net);
            WSACleanup();//中止Windows Sockets DLL的使用
            ExitProcess(1);   
    }   
      
    for(j=0;j<8;j++)   
    {   
    //receive   data   
        ret=recv(net,szBuffer,2048,0);   
        if((ret==0)   ||   (ret==SOCKET_ERROR))
        {
            printf("\nrecv()   failed.ErrorCode:%d.",WSAGetLastError());   
            break;
        }   
    /*echo   reveived   data   
        printf("\n");   
        for(i=0;i<ret;i++)   
        printf("%c",szBuffer[i]);
        */
        if(j==7)   
        break;   
    //echo   send   data   
        printf("%s",szSend[j]);   
    //send   data   
        ret=send(net,szSend[j],strlen(szSend[j]),0);   
        if((ret==0)   ||   (ret==SOCKET_ERROR))
        {
            printf("\nsend()   failed.ErrorCode:%d.",WSAGetLastError());
            break;
        }
        }   
    //free   resource   
        closesocket(net);   
        WSACleanup();   
    return   0;   
  }   
  int   main(int   argc,char   **argv)   
  {   
    HANDLE   hThread;   
    DWORD   dwThreadId;   
    int   i;   
    if(argc!=7)   
    {   
            usage(argv[0]);
            return   1;

    }

    //gets   user   input   
    strncpy(server,argv[1],sizeof(server));   
    strncpy(to,argv[2],sizeof(to));   
    strncpy(from,argv[3],sizeof(from));   
    strncpy(subject,argv[4],sizeof(subject));   
    strncpy(data,argv[5],sizeof(data));   
    times=atoi(argv[6]);   
    strncpy((char   *)&sa,   "",   sizeof   sa);

    //fill   the   struct   
    sa.sin_family=AF_INET;   
    sa.sin_port=htons(25);   
    sa.sin_addr.s_addr=inet_addr(server);   
    ///built   send   data   
    strcpy(szSend[0],"HELO\n");   
    strcpy(szSend[1],"MAIL   FROM:<");   
    strncat(szSend[1],from,sizeof(from));   
    strcat(szSend[1],">\n");   
    strcpy(szSend[2],"RCPT   TO:<");   
    strncat(szSend[2],to,sizeof(to));   
    strcat(szSend[2],">\n");   
    strcpy(szSend[3],"DATA\n");   
    strcpy(szSend[4],"Subject:");   
    strncat(szSend[4],subject,sizeof(subject));   
    strcat(szSend[4],"\nFrom:");   
    strncat(szSend[4],from,sizeof(from));   
    strcat(szSend[4],"\n\n");   
    strncat(szSend[4],data,sizeof(data));   
    strcat(szSend[4],"\n.\n");   
    strcpy(szSend[5],"SEND\n");   
    strcpy(szSend[6],"QUIT\n");   
    for(i=0;i<times;i++)   
    {   
            hThread=CreateThread(NULL,0,SendMail,(PVOID)NULL,0,&dwThreadId);   
              if(hThread==NULL)
            {
                printf("\nCreate   thread   error.");
                break;
            }
            Sleep(20);
            CloseHandle(hThread);
            printf("\nHave   been   send   %d   Email.",i+1);   
    }   
    printf("\nWait   5   seconds   to   exit.");   
    Sleep(5000);   
    printf("\nComplete.");   
    return   0;   
  }
编译一个warning
VC里调试出现 Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\ws2_32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\advapi32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\rpcrt4.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\secur32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\msvcrt.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\ws2help.dll', no matching symbolic information found.
The thread 0x9CC has exited with code 1 (0x1).
The program 'C:\Documents and Settings\Administrator\桌面\Debug\Try.exe' has exited with code 1 (0x1).

[[it] 本帖最后由 Waiting_alone 于 2008-10-29 20:08 编辑 [/it]]
搜索更多相关主题的帖子: socket 
2008-10-29 20:07
快速回复:大家看下这个程序哪里出问题了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.078777 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved