gpt4 book ai didi

sockets - 无法修复错误10038 WSAENOTSOCK

转载 作者:行者123 更新时间:2023-12-03 12:04:46 25 4
gpt4 key购买 nike

我正在尝试学习Berkeley Socket编程,但是我真的被困在这里:
根据MSDN,我的服务器和客户端程序都出现10038错误,这意味着:“尝试对不是套接字的对象进行了操作”,但套接字却是“套接字”。这是服务器/客户端代码:

我的客户 :

#include <winsock.h>
#include <stdio.h>
#include <string.h>

#define BUFFSIZE 32

void DieWithError(char *errorMessage);

int main(int argc, char* argv[]){

int sock, bytesLen;
struct sockaddr_in servAddr;
unsigned short servPort =13;
char * servIP;
char buff[BUFFSIZE+1];
WSADATA wsaData;

if(argc == 2){
servIP = argv[1];
}

if(WSAStartup(MAKEWORD(2,0), &wsaData) != 0){
puts("ERROR WSA");
}

if(sock=socket(AF_INET, SOCK_STREAM, 0) < 0){
puts("ERROR socket");
}

memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(servPort);
servAddr.sin_addr.s_addr = inet_addr(&servIP);

if(connect(sock, (struct sockaddr *)&servAddr, sizeof(servAddr)<0)){
DieWithError("connect Error");
}

while(bytesLen=read(sock, buff, BUFFSIZE)){
buff[bytesLen]= '\n';
}

printf("Heure : %s",buff);

closesocket(sock);
WSACleanup();

return 0;
}

void DieWithError(char *errorMessage)
{
fprintf(stderr,"%s: %d\n",
errorMessage, WSAGetLastError());
exit(1);
}

和我的服务器:
#include <winsock.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>


#define MAXLINE 32

void DieWithError(char *errorMessage);

int main(){

int sock, sockc;
struct sockaddr_in servAddr;
unsigned short servPort =13;
time_t ticks;
WSADATA wsaData;
char buff[MAXLINE];
char * servIP;

if(WSAStartup(MAKEWORD(2,0), &wsaData) != 0){
puts("ERROR WSA");
}

if(sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) < 0){
puts("ERROR socket");
}
servIP = "10.1.1.3";
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(servPort);
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);

if(bind(sock,(struct sockaddr *)&servAddr, sizeof(servAddr) < 0)){
DieWithError("bind)()");
}

if(listen(sock,MAXQUEUE) < 0){
DieWithError("listen ()");
}

while(1){

sockc = accept(sock, (struct sockaddr *)NULL, NULL);
ticks = time(NULL);
snprintf(buff,sizeof(buff),"%s",ctime(&ticks));

write(buff, sizeof(buff), MAXLINE);
}

return 0;
}

void DieWithError(char *errorMessage)
{
fprintf(stderr,"%s: %d\n",
errorMessage, WSAGetLastError());
exit(1);
}

每次启动时,我在客户端的 connect()函数上具有10038,在服务器上的 bind()函数上具有10038。

谢谢你的帮助。

最佳答案

与其他平台不同,Windows不使用文件描述符来表示套接字。它使用实际的内核对象。其他平台使用int表示打开的文件描述符,从而表示套接字。 Windows使用SOCKET类型代替,它是UINT_PTR的typedef(32位的unsigned int,64位的unsigned __int64),因此它可以容纳对象句柄。 Windows上的socket()函数在错误时返回INVALID_SOCKET(即(SOCKET)(~0)),而其他平台则返回(int)-1。您需要确保对此进行说明,以免在Windows上对合法的套接字句柄进行切片/截断。

当您的客户端调用inet_addr()时,它将传递一个char**,其中应包含char*。您的编译器应该已经报告了一个错误。

由于套接字在Windows上未表示为文件描述符,因此不能在Windows套接字上使用read()write()函数。您必须改为使用recv()/WSARecv()send()/WSASend()函数。并且您需要处理函数可以在错误时返回SOCKET_ERROR(-1)的情况。

尝试类似这样的方法:

客户 :

#include <winsock.h>
#include <stdio.h>
#include <string.h>

#define BUFFSIZE 32

void DieWithErrorCode(char *errorMessage, int errCode);
void DieWithError(char *errorMessage, int *errCode = NULL);

int main(int argc, char* argv[])
{
SOCKET sock;
int ret;
struct sockaddr_in servAddr;
unsigned short servPort = 13;
char *servIP;
char buff[BUFFSIZE];
WSADATA wsaData;

if (argc != 2) {
DieWithErrorCode("ERROR argc", WSAEINVAL);
}
servIP = argv[1];

ret = WSAStartup(MAKEWORD(2,0), &wsaData);
if (ret != 0) {
DieWithErrorCode("ERROR WSAStartup", ret);
}

sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
DieWithError("ERROR socket");
}

memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(servPort);
servAddr.sin_addr.s_addr = inet_addr(servIP);
if (servAddr.sin_addr.s_addr == INET_NONE) {
DieWithErrorCode("ERROR inet_addr", WSAEINVAL);
}

if (connect(sock, (struct sockaddr *)&servAddr, sizeof(servAddr)) == SOCKET_ERROR) {
DieWithError("ERROR connect");
}

while(1) {
ret = recv(sock, buff, BUFFSIZE, 0);
if (ret == SOCKET_ERROR) {
DieWithError("ERROR recv");
}

if (ret == 0) {
fprintf(stderr, "disconnected\n");
break;
}

printf("Heure : %.*s\n", ret, buff);
}

closesocket(sock);
WSACleanup();

return 0;
}

void DieWithErrorCode(char *errorMessage, int errCode)
{
fprintf(stderr, "%s: %d\n", errorMessage, errCode);
exit(1);
}

void DieWithError(char *errorMessage)
{
DieWithErrorCode(errorMessage, WSAGetLastError());
}

服务器:
#include <winsock.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

#define MAXLINE 32

void DieWithErrorCode(char *errorMessage, int errCode);
void DieWithError(char *errorMessage);

int main()
{
SOCKET sock, sockc;
struct sockaddr_in servAddr;
unsigned short servPort = 13;
time_t ticks;
WSADATA wsaData;
char buff[MAXLINE];
int ret, buflen;

ret = WSAStartup(MAKEWORD(2,0), &wsaData);
if (ret != 0) {
DieWithErrorCode("ERROR WSA", ret);
}

sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
DieWithError("ERROR socket");
}

memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(servPort);
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);

if (bind(sock, (struct sockaddr *)&servAddr, sizeof(servAddr)) == SOCKET_ERROR) {
DieWithError("ERROR bind");
}

if (listen(sock, MAXQUEUE) == SOCKET_ERROR) {
DieWithError("ERROR listen");
}

while(1) {
sockc = accept(sock, NULL, NULL);
if (sockc == INVALID_SOCKET) {
DieWithError("ERROR accept");
}

ticks = time(NULL);
buflen = snprintf(buff, sizeof(buff), "%s", ctime(&ticks));
if (send(sockc, buff, buflen, 0) == SOCKET_ERROR) {
fprintf(stderr, "ERROR send: %d\n", WSAGetLastError());
}

closesocket(sockc);
}

closesocket(sock);
WSACleanup();

return 0;
}

void DieWithErrorCode(char *errorMessage, int errCode)
{
fprintf(stderr,"%s: %d\n", errorMessage, errCode);
exit(1);
}

void DieWithError(char *errorMessage)
{
DieWithErrorCode(char *errorMessage, WSAGetLastError());
}

关于sockets - 无法修复错误10038 WSAENOTSOCK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31173661/

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