gpt4 book ai didi

c 套接字无法连接到服务器

转载 作者:太空宇宙 更新时间:2023-11-04 01:58:08 25 4
gpt4 key购买 nike

我已尝试向服务器发送一个字符串,服务器会将该字符串发回给我。

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

输入的argv没问题

argv[1] = "54.152.161.133" (server ip)
argv[2] = "6713" (port)
argv[3] = string

#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);
}

最佳答案

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

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)

如果您只是想检查标志的值。

关于c 套接字无法连接到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30940559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com