首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C套接字-不能连接到服务器

C套接字-不能连接到服务器
EN

Stack Overflow用户
提问于 2015-06-19 14:22:07
回答 2查看 66关注 0票数 1

我试图向服务器发送一个字符串,而服务器将将该字符串送回给我。

问题是没有连接(函数连接返回SOCKET_ERROR)

输入的argv很好

argv1 = "54.152.161.133“(服务器ip) argv2 = "6713“(港口) argv3 = string

代码语言:javascript
复制
#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);
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-19 14:43:20

按照顺序,您创建了套接字:

代码语言:javascript
复制
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

然后你关闭了它:

代码语言:javascript
复制
/*close socket code*/
cResult = closesocket(s);

当你试图把它连接到某件事之后:

代码语言:javascript
复制
cResult = connect(s, (struct sockaddr *) &clientService, sizeof(clientService));

关闭套接字后,将其视为已删除的套接字。只有在程序的末尾才能关闭它。

顺便说一句,你应该考虑替换这一行:

代码语言:javascript
复制
if (flag = 0)

通过这一条:

代码语言:javascript
复制
if (flag == 0)

如果你只是想检查旗子的价值。

票数 2
EN

Stack Overflow用户

发布于 2015-06-19 14:30:28

您的套接字在connect调用时关闭。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30940559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档