在c+中连接和发送数据时出错+;使用winsock2.h 我一直在尝试在C++中执行套接字编程。我在网上查过这段代码,根据网站上的信息,它运行良好。但我在通过连接发送数据时收到错误。下面是代码片段 #include <iostream
在c+中连接和发送数据时出错+;使用winsock2.h <>我一直在尝试在C++中执行套接字编程。我在网上查过这段代码,根据网站上的信息,它运行良好。但我在通过连接发送数据时收到错误。下面是代码片段在c+中连接和发送数据时出错+;使用winsock2.h 我一直在尝试在C++中执行套接字编程。我在网上查过这段代码,根据网站上的信息,它运行良好。但我在通过连接发送数据时收到错误。下面是代码片段 #include <iostream,c++,sockets,winsock2,C++,Sockets,Winsock2,在c+中连接和发送数据时出错+;使用winsock2.h 我一直在尝试在C++中执行套接字编程。我在网上查过这段代码,根据网站上的信息,它运行良好。但我在通过连接发送数据时收到错误。下面是代码片段 #include <iostream> #include <winsock2.h> #include <string> #include <conio.h> int main() { WSAData version;
#include <iostream>
#include <winsock2.h>
#include <string>
#include <conio.h>
int main()
{
WSAData version; //We need to check the version.
WORD mkword=MAKEWORD(2,2);
int what=WSAStartup(mkword,&version);
if(what!=0){
std::cout<<"This version is not supported! - \n"<<WSAGetLastError() <<std::endl;
}
else{
std::cout<<"Good - Everything fine!\n"<<std::endl;
}
SOCKET u_sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(u_sock==INVALID_SOCKET)
std::cout<<"Creating socket fail\n";
else
std::cout<<"It was okay to create the socket\n";
//Socket address information
sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr("192.168.43.194");
addr.sin_port=htons(80);
/*==========Addressing finished==========*/
//Now we connect
int conn=connect(u_sock,(SOCKADDR*)&addr,sizeof(addr));
if(conn==SOCKET_ERROR){
std::cout<<"Error - when connecting "<<WSAGetLastError()<<std::endl;
closesocket(u_sock);
WSACleanup();
}
//Send some message to remote host
char* mymsg="GET / HTTP/1.1\r\n\r\n";
char vect[512]={0};
int smsg=send(u_sock,mymsg,sizeof(mymsg),0);
if(smsg==SOCKET_ERROR){
std::cout<<"Error: "<<WSAGetLastError()<<std::endl;
WSACleanup();
}
int get=recv(u_sock,vect,512,0);
if(get==SOCKET_ERROR){
std::cout<<"Error in Receiving: "<<WSAGetLastError()<<std::endl;
}
std::cout<<vect<<std::endl;
closesocket(u_sock);
_getch();
return 0;
}
#包括
#包括
#包括
#包括
int main()
{
WSAData version;//我们需要检查版本。
单词mkword=MAKEWORD(2,2);
int what=WSAStartup(mkword和版本);
如果(什么!=0){
std::coutw你认为什么是sizeof(char*)
?正确。使用strlen(mymsg)
。顺便说一句:好名字-u_sock
:)。