关于 Eclipse 开发C++ Socket ,在开发的过程中 用WinGW 平台编译,
示例server端:
#include <winsock2.h>
#include <mswsock.h> #include <iostream> #include <windows.h> using namespace std;#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "mswsock.lib")DWORD IniSOCKDLL()
{ WORD wVersionRequested; WSADATA wsaData; int err=0;wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData ); return err; }int main(int argc, char* argv[])
{ IniSOCKDLL(); SOCKET sc=WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, NULL);SOCKADDR_IN addr;
int len; addr.sin_family=AF_INET; addr.sin_addr.s_addr = inet_addr("127.0.0.1"); addr.sin_port = htons(1002); len=sizeof(addr);connect(sc, (struct sockaddr *)&addr, len);
char buff[1024];
ZeroMemory(buff,1024);cout<<"邮包开始发送"<<endl;
/*for(int i=0;i<=100;i++)
{ if(i==0) {*/ memcpy(buff,"邮包从南京出发",14); send(sc,buff,14,0); Sleep(1000); /*} else if(i<20) {*/ memcpy(buff,"邮包在南京和镇江之间",20); send(sc,buff,20,0); Sleep(1000);memcpy(buff,"邮包到达镇江",12);
send(sc,buff,12,0); Sleep(1000);memcpy(buff,"邮包在镇江和常州之间",20);
send(sc,buff,20,0); Sleep(1000);memcpy(buff,"邮包到达常州",12);
send(sc,buff,12,0); Sleep(1000); memcpy(buff,"邮包在常州和无锡之间",20); send(sc,buff,20,0); Sleep(1000);memcpy(buff,"邮包到达无锡",12);
send(sc,buff,12,0); Sleep(1000);memcpy(buff,"邮包在无锡和苏州之间",20);
send(sc,buff,20,0); Sleep(1000);memcpy(buff,"邮包到达苏州",12);
send(sc,buff,12,0); Sleep(1000);memcpy(buff,"邮包在苏州和上海之间",20);
send(sc,buff,20,0); Sleep(1000); /*} else {*/ memcpy(buff,"邮包到达上海",12); send(sc,buff,12,0); Sleep(1000);/* recv(ss,buff,12,0);
cout<<buff<<endl;*/closesocket(sc);
return 0; }
会发现Error] C:\Users\HEIORAY\Documents\C-Free\Projects\PING\pi.c:277: undefined reference to
程序开始时已经导入winsock之类的库,同时#pragma comment(lib,"ws2_32.lib"),但依旧不行,无法消除错误。后来上网搜了一些方法,说是在工程设置中有个连接,在里面加入winsock32就可以了,试过之后的确解决了一些问题。
但是下面的错误还是没有消除,
[Error] C:\Users\HEIORAY\Documents\C-Free\Projects\PING\pi.c:277: undefined reference to
最后在连接里面再填入一个ws2_32就可以了。如果大家有类似的错误可以试一下。
问题解决 可以进行了