你好我试着用参数打开socket(你知道argv和argc)但是由于某种原因,程序显示错误WSAStratup @ 8和类似的东西。 我认为问题与论据的收入有关。
如果你能看到并帮助我理解为什么会发生这种情况,我会很高兴。
我的代码 -
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
int main(int argc,char *argv[])
{
WSADATA info;
int err, s, cResult;
char ch[10] = "";
char cl[10] = "";
strcpy(ch, argv[3]);
err = WSAStartup(MAKEWORD(2, 0), &info);
if (err != 0)
{
printf("WSAtartup faild\n");
return(1);
}
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
printf("Error creating = %d\n", WSAGetLastError());
}
else
{
printf("socket function succeeded\n");
}
SOCKADDR_IN clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr(argv[1]);
clientService.sin_port = htons(atoi(argv[2]));
cResult = connect(s, (struct sockaddr *) &clientService, sizeof (clientService));
if (cResult == SOCKET_ERROR)
{
printf("connect failed error: %ld\n", WSAGetLastError());
}
send(s, ch, 10, 0);
recv(s, cl, 10, 0);
puts(ch);
puts(cl);
if (strcmp(ch, cl) == 0)
{
printf(" Same\n");
}
else
{
printf("Not same\n");
}
cResult = closesocket(s);
if (cResult == SOCKET_ERROR)
{
printf("closesocket function failed error %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
system("PAUSE");
}
答案 0 :(得分:1)
从您的问题不清楚什么是失败,但要正确初始化Winsock 2支持的功能,您想要调用
WSAStartup(WINSOCK_VERSION, &wsadata);
在Winsock2.h中将WINSOCK_VERSION定义为:
#define WINSOCK_VERSION MAKEWORD(2,2)
您还需要与ws2_32.lib链接以使用Winsock 2函数(Winsock 1.1非常老 - 该版本从wsock32.lib导出)。
答案 1 :(得分:0)
我在我的Visual Studio中运行了你的程序,运行正常。您应该考虑以下几点:在调用程序时,您应该指定IP,端口号和一些测试消息。
myprogram 127.0.0.1 1024 "test message"
此外,如果执行或出错,则应检查每个套接字命令,如send()
和recv()
。查看修改后的程序。最后,您应该将您的程序与wsock32.lib
库链接起来。转到项目属性并将此库添加到链接器页面中的其他依赖项,具体取决于您的语言,我有西班牙语,它说“ Dependencias adicionales ”也许你的Visual Studio会说些不同的东西。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
int main(int argc,char *argv[])
{
WSADATA info;
int err, s, cResult;
char ch[10] = "";
char cl[10] = "";
// Check that the appropiate arguments has been given
if (argc < 4) return -1;
// Copy up the size of ch
strncpy(ch, argv[3], sizeof(ch));
err = WSAStartup(MAKEWORD(2, 0), &info);
if (err != 0)
{
printf("WSAtartup failed: %ld\n", WSAGetLastError());
system("PAUSE");
return -1;
}
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
printf("Error creating = %ld\n", WSAGetLastError());
system("PAUSE");
return -1;
}
else
{
printf("socket function succeeded\n");
}
SOCKADDR_IN clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr(argv[1]);
clientService.sin_port = htons(atoi(argv[2]));
cResult = connect(s, (struct sockaddr *) &clientService, sizeof (clientService));
if (cResult == SOCKET_ERROR)
{
printf("connect failed error: %ld\n", WSAGetLastError());
system("PAUSE");
return -1;
}
if (send(s, ch, 10, 0) > 0)
{
puts(ch);
if (recv(s, cl, 10, 0) > 0)
{
puts(cl);
if (strcmp(ch, cl) == 0)
{
printf(" Same\n");
}
else
{
printf("Not same\n");
}
}
else
{
printf("error in recv() %ld\n", WSAGetLastError());
}
}
else
{
printf("error in send() %ld\n", WSAGetLastError());
}
cResult = closesocket(s);
if (cResult == SOCKET_ERROR)
{
printf("closesocket function failed error %ld\n", WSAGetLastError());
}
WSACleanup();
system("pause");
return 0;
}