2015-06-19 113 views
1

我試圖發送服務器一個字符串,服務器將發回我的字符串。c套接字無法連接到服務器

的問題是,不存在連接(連接函數返回SOCKET_ERROR

輸入的argv是細

的argv [1] = 「54.152.161.133」(服務器IP)
的argv [2] = 「6713」(端口)
的argv [3] =串

#include "stdafx.h" 
#include<stdio.h> 
#include<stdlib.h> 
#include<winsock2.h> 
#include<windows.h> 
#include<string.h> 

//#define DEST_IP "54.152.161.133" 
//#define DEST_PORT 6713 
// SOCKET WSAAPI socket(int af, int type, int protocol); 
//int connect(SOCKET s, const struct sockaddr* addr, int numBytes); 
//int send(SOCKET s, const char* buf,int len,int flags); 
//int recv(SOCKET s, char* buf, int len, int flags); 
//int closesocket(SOCKET s); 
//int WSACleanup(); 
int main(int argc, char** argv) 
{ 
    int cResult, flag = 0, len,s; 
    char str2[10]; 
    WSADATA info; 
    struct sockaddr_in clientService; 
    unsigned short a = (unsigned short)strtoul(argv[2], NULL, 0);//convert from char* to short 
    //config sockets 
    int err; 
    err = WSAStartup(MAKEWORD(2, 0), &info); 
    if (err != 0) 
    { 
     printf("WSAStartup faild with error: %d\n", err); 
     flag = 1; 
    } 
    //make empty socket 
    if (flag == 0) 
    { 
     s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
     if (s == INVALID_SOCKET)//Constant used for errors in socket() 
     { 
      //WSAGetLastError() get error code 
      printf("Error creating socket = %d\n", WSAGetLastError()); 
     } 
     else 
     { 
      printf("Socket function succeeded\n"); 
      /*close socket code*/ 
      cResult = closesocket(s); 
      if (cResult == SOCKET_ERROR) 
      { 
       printf("Closesocket function faild with error %ld\n", WSAGetLastError()); 

      } 
     } 
     //config the socket 
     sockaddr_in clientService; 
     clientService.sin_family = AF_INET; 
     clientService.sin_addr.s_addr = inet_addr(argv[1]); 
     clientService.sin_port = htons(a); 
     //ask for connection 
     cResult = connect(s, (struct sockaddr *) &clientService, sizeof(clientService)); 
     if (cResult == SOCKET_ERROR) 
     { 
      printf("Connect function failed with error: %ld\n", WSAGetLastError()); 
      cResult = closesocket(s); 
      if (cResult == SOCKET_ERROR) 
      { 
       printf("Closesocket function faild with error %ld\n", WSAGetLastError()); 
      } 
      /*Erase WSADATA code*/ 
      WSACleanup(); 
      flag = 1; 
     } 
     if (flag = 0) 
     { 
      //send info throw socket 
      len = strlen(argv[3]); 
      cResult = send(s, *(argv + 3), len + 1, 0); 
      if (cResult == SOCKET_ERROR) 
      { 
       printf("Sending data to the server has failed\n"); 
       /* Close socket code*/ 
       cResult = closesocket(s); 
       if (cResult == SOCKET_ERROR) 
       { 
        printf("Closesocket function faild with error %ld\n", WSAGetLastError()); 
       } 
       /*Erase WSADATA code (we will see later) */ 
       WSACleanup(); 
       flag = 1; 
      } 
      if (flag == 0) 
      { 
       //gets data from socket 
       cResult = recv(s, str2, len + 1, 0); 
       if (cResult == SOCKET_ERROR) 
       { 
        printf("Recving data from the server has failed\n"); 
        /* Close socket code*/ 
        cResult = closesocket(s); 
        printf("Closesocket function faild with error %ld\n", WSAGetLastError()); 
        /*Erase WSADATA code (we will see later) */ 
        WSACleanup(); 
        flag = 1; 
       } 
       else if (cResult == 0) 
       { 
        printf("The server closed the connection\n"); 
        flag = 1; 
       } 
       if (flag == 0) 
       { 
        printf(" string that sent to the server:%s\nstring that recv from the server:%s", argv[3], str2); 
        if (strcmp(argv[3], str2) == 0) 
        { 
         printf("The string that sent to the server\nand the string that recv from the server are the same\n"); 

        } 
        else 
        { 
         printf("The string that sent to the server\nand the string that recv from the server are not the same\n"); 
        } 
        cResult = closesocket(s); 
        if (cResult == SOCKET_ERROR) 
        { 
         printf("Closesocket function faild with error %ld\n", WSAGetLastError()); 
        } 
        WSACleanup(); 

       } 
      } 


     } 
    } 
    system("PAUSE"); 
    return (0); 
} 

回答

2

爲了,你所創建的套接字:

s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 

然後你關閉了它:

/*close socket code*/ 
cResult = closesocket(s); 

你試圖將它連接到東西后:

cResult = connect(s, (struct sockaddr *) &clientService, sizeof(clientService)); 

考慮一個套接字一旦被關閉就被刪除。只有在程序的最後才能關閉它。

順便說一下,你應該考慮替換此行:

if (flag = 0) 

通過這一個:

if (flag == 0) 

如果你只是想檢查該標誌的值。

1

您的套接字在撥打connect時關閉。

相關問題