C++TCP通信服务端代码问题
代码如下#include<winsock2.h>#pragmacomment(lib,"ws2_32")intmain(intargc,char*argv[]){WSAD...
代码如下
#include<winsock2.h>
#pragma comment (lib,"ws2_32")
int main(int argc, char* argv[])
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
sockaddr_in socketaddr;
sockaddr.sin_family = PF_INTF;
sockaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
sockaddr.sin_port = htons(827);
bind(s, (SOCKADDR*)&sockaddr, sizeof(SOCKETADDR));
listen(s,1);
SOCKADDR clientAddr;
int nSize = sizeof(SOCKADDR);
SOCKET clientSock;
clientSock = accept(s,(SOCKADDR*)&clientAddr,&nSize);
send(clientSock,"hello world!\r\n",strlen("hello world!\r\n")+sizeof(char),NULL);
closesocket(clientSock);
closesocket(s);
WSACleanup();
return 0;
}
调试如下
--------------------Configuration: TCP HW - Win32 Debug--------------------
Compiling...
TCP HW S.CPP
c:\users\administrator.pc-20140912zwna\desktop\app\tcp hw\tcp hw s.cpp(12) : error C2143: syntax error : missing ';' before '.'
………………(中间都是这种)
c:\users\administrator.pc-20140912zwna\desktop\app\tcp hw\tcp hw s.cpp(14) : error C2143: syntax error : missing ';' before '.'
c:\users\administrator.pc-20140912zwna\desktop\app\tcp hw\tcp hw s.cpp(16) : error C2275: 'sockaddr' : illegal use of this type as an expression
c:\program files\microsoft visual studio\vc98\include\winsock2.h(512) : see declaration of 'sockaddr'
c:\users\administrator.pc-20140912zwna\desktop\app\tcp hw\tcp hw s.cpp(16) : error C2065: 'SOCKETADDR' : undeclared identifier
Error executing cl.exe.
TCP HW.exe - 8 error(s), 0 warning(s) 展开
#include<winsock2.h>
#pragma comment (lib,"ws2_32")
int main(int argc, char* argv[])
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
sockaddr_in socketaddr;
sockaddr.sin_family = PF_INTF;
sockaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
sockaddr.sin_port = htons(827);
bind(s, (SOCKADDR*)&sockaddr, sizeof(SOCKETADDR));
listen(s,1);
SOCKADDR clientAddr;
int nSize = sizeof(SOCKADDR);
SOCKET clientSock;
clientSock = accept(s,(SOCKADDR*)&clientAddr,&nSize);
send(clientSock,"hello world!\r\n",strlen("hello world!\r\n")+sizeof(char),NULL);
closesocket(clientSock);
closesocket(s);
WSACleanup();
return 0;
}
调试如下
--------------------Configuration: TCP HW - Win32 Debug--------------------
Compiling...
TCP HW S.CPP
c:\users\administrator.pc-20140912zwna\desktop\app\tcp hw\tcp hw s.cpp(12) : error C2143: syntax error : missing ';' before '.'
………………(中间都是这种)
c:\users\administrator.pc-20140912zwna\desktop\app\tcp hw\tcp hw s.cpp(14) : error C2143: syntax error : missing ';' before '.'
c:\users\administrator.pc-20140912zwna\desktop\app\tcp hw\tcp hw s.cpp(16) : error C2275: 'sockaddr' : illegal use of this type as an expression
c:\program files\microsoft visual studio\vc98\include\winsock2.h(512) : see declaration of 'sockaddr'
c:\users\administrator.pc-20140912zwna\desktop\app\tcp hw\tcp hw s.cpp(16) : error C2065: 'SOCKETADDR' : undeclared identifier
Error executing cl.exe.
TCP HW.exe - 8 error(s), 0 warning(s) 展开
1个回答
展开全部
#include<winsock2.h>
#pragma comment (lib,"ws2_32")
int main(int argc, char* argv[])
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
/*struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
*/
sockaddr_in socketaddr;
socketaddr.sin_family = PF_INET;
socketaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
socketaddr.sin_port = htons(827);
bind(s, (SOCKADDR*)&socketaddr, sizeof(SOCKADDR));
listen(s,1);
SOCKADDR clientAddr;
int nSize = sizeof(SOCKADDR);
SOCKET clientSock;
clientSock = accept(s,(SOCKADDR*)&clientAddr,&nSize);
send(clientSock,"hello world!\r\n",strlen("hello world!\r\n")+sizeof(char),NULL);
closesocket(clientSock);
closesocket(s);
WSACleanup();
return 0;
}
这是修改后的代码。兄弟,打字认真点,都是单词拼写错误!!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询