Chinaunix首页 | 论坛 | 博客
  • 博客访问: 251718
  • 博文数量: 44
  • 博客积分: 1795
  • 博客等级: 上尉
  • 技术积分: 465
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-04 12:02
文章分类

全部博文(44)

文章存档

2015年(1)

2011年(1)

2009年(7)

2008年(2)

2007年(7)

2006年(26)

我的朋友

分类: C/C++

2006-08-01 16:50:30

// Module Name: tcpclient.cpp
//
// Description:
//
//    This sample illustrates how to develop a simple TCP client application
//    that can send a simple "hello" message to a TCP server listening on port 5150.
//    This sample is implemented as a console-style application and simply prints
//    status messages a connection is made and when data is sent to the server.
//
// Compile:
//
//    cl -o tcpclient tcpclient.cpp ws2_32.lib
//
// Command Line Options:
//
//    tcpclient.exe
//
#include
#include
void std_err(int type);
void main(int argc, char **argv)
{
   WSADATA              wsaData;
   SOCKET               s;
   SOCKADDR_IN          ServerAddr;
   int                  Port = 10001;
   int                  Ret;
   int     lastErr;
   if (argc <= 1)
   {
      printf("USAGE: tcpclient .\n");
      return;
   }
   // Initialize Winsock version 2.2
   if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
   {
      // NOTE: Since Winsock failed to load we cannot use WSAGetLastError
      // to determine the error code as is normally done when a Winsock
      // API fails. We have to report the return status of the function.
      printf("WSAStartup failed with error %d\n", Ret);
      return;
   }
  
   // Create a new socket to make a client connection.
 
   if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))
       == INVALID_SOCKET)
   {
   lastErr = WSAGetLastError();
   std_err(lastErr);
      WSACleanup();
      return;
   }
 
   // Setup a SOCKADDR_IN structure that will be used to connect
   // to a listening server on port 5150. For demonstration
   // purposes, we required the user to supply an IP address
   // on the command line and we filled this field in with the
   // data from the user.
   ServerAddr.sin_family = AF_INET;
   ServerAddr.sin_port = htons(Port);   
   ServerAddr.sin_addr.s_addr = inet_addr(argv[1]);
   // Make a connection to the server with socket s.
   printf("We are trying to connect to %s:%d...\n",
          inet_ntoa(ServerAddr.sin_addr), htons(ServerAddr.sin_port));
   if (connect(s, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr))
       == SOCKET_ERROR)
   {
   lastErr = WSAGetLastError();
   std_err(lastErr);
      closesocket(s);
      WSACleanup();
      return;
   }
   printf("Our connection succeeded.\n");
     
   // At this point you can start sending or receiving data on
   // the socket s. We will just send a hello message to the server.
   printf("We will now try to send a hello message.\n");
   if ((Ret = send(s, "Hello", 5, 0)) == SOCKET_ERROR)
   {
   lastErr = WSAGetLastError();
   std_err(lastErr);
      closesocket(s);
      WSACleanup();
      return;
   }
   printf("We successfully sent %d byte(s).\n", Ret);
   // When you are finished sending and receiving data on socket s,
   // you should close the socket.
   printf("We are closing the connection.\n");
   closesocket(s);
   // When your application is finished handling the connection, call
   // WSACleanup.
   WSACleanup();
}
void std_err(int type) {
    char    *error;
    switch(type) {
        case 10004: error = "Interrupted system call"; break;
        case 10009: error = "Bad file number"; break;
        case 10013: error = "Permission denied"; break;
        case 10014: error = "Bad address"; break;
        case 10022: error = "Invalid argument (not bind)"; break;
        case 10024: error = "Too many open files"; break;
        case 10035: error = "Operation would block"; break;
        case 10036: error = "Operation now in progress"; break;
        case 10037: error = "Operation already in progress"; break;
        case 10038: error = "Socket operation on non-socket"; break;
        case 10039: error = "Destination address required"; break;
        case 10040: error = "Message too long"; break;
        case 10041: error = "Protocol wrong type for socket"; break;
        case 10042: error = "Bad protocol option"; break;
        case 10043: error = "Protocol not supported"; break;
        case 10044: error = "Socket type not supported"; break;
        case 10045: error = "Operation not supported on socket"; break;
        case 10046: error = "Protocol family not supported"; break;
        case 10047: error = "Address family not supported by protocol family"; break;
        case 10048: error = "Address already in use"; break;
        case 10049: error = "Can't assign requested address"; break;
        case 10050: error = "Network is down"; break;
        case 10051: error = "Network is unreachable"; break;
        case 10052: error = "Net dropped connection or reset"; break;
        case 10053: error = "Software caused connection abort"; break;
        case 10054: error = "Connection reset by peer"; break;
        case 10055: error = "No buffer space available"; break;
        case 10056: error = "Socket is already connected"; break;
        case 10057: error = "Socket is not connected"; break;
        case 10058: error = "Can't send after socket shutdown"; break;
        case 10059: error = "Too many references, can't splice"; break;
        case 10060: error = "Connection timed out"; break;
        case 10061: error = "Connection refused"; break;
        case 10062: error = "Too many levels of symbolic links"; break;
        case 10063: error = "File name too long"; break;
        case 10064: error = "Host is down"; break;
        case 10065: error = "No Route to Host"; break;
        case 10066: error = "Directory not empty"; break;
        case 10067: error = "Too many processes"; break;
        case 10068: error = "Too many users"; break;
        case 10069: error = "Disc Quota Exceeded"; break;
        case 10070: error = "Stale NFS file handle"; break;
        case 10091: error = "Network SubSystem is unavailable"; break;
        case 10092: error = "WINSOCK DLL Version out of range"; break;
        case 10093: error = "Successful WSASTARTUP not yet performed"; break;
        case 10071: error = "Too many levels of remote in path"; break;
        case 11001: error = "Host not found"; break;
        case 11002: error = "Non-Authoritative Host not found"; break;
        case 11003: error = "Non-Recoverable errors: FORMERR, REFUSED, NOTIMP"; break;
        case 11004: error = "Valid name, no data record of requested type"; break;
        default: error = strerror(errno); break;
    }
    fprintf(stderr, "\nError: %s\n", error);
    //exit(1);
}
阅读(2923) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-04-21 10:03:47

kbjiyflghcvfdr5fvfgvfgbdhdgfdcv