#include <iostream.h>
#include <stdio.h>
#include <Winsock2.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested=MAKEWORD(1,1);
err=WSAStartup(wVersionRequested,&wsaData);/*初始化WINSOCK库*/
if(err!=0)
{ return;}/*如果调用成功则返回0,不成功即跳出*/
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0); /*创建套接字,TCP/IP协议参数为AF_INET,
流套接字参数为SOCK_STRSAM,0为默认的连接方式*/
SOCKADDR_IN addrSrv;
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000); /*指明端口号*/
addrSrv.sin_addr.s_addr=htonl(INADDR_ANY); /*主机地址任意*/
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(addrSrv)); /*绑定套接字与端口*/
listen(sockSrv,1);
SOCKADDR_IN addrClient;
int addrlen;
addrlen=sizeof(addrClient);
SOCKET socketClient=accept(sockSrv,(SOCKADDR*)&addrClient,&addrlen);/*创建另外1个套接字
socketClient与客户套界字建立连接*/
char recvBuf[50];
recv(socketClient,recvBuf,50,0);
cout << recvBuf << endl;
char sendBuf[50];
cin >> sendBuf;
int len;
len=strlen(sendBuf)+1;
send(socketClient,sendBuf,len,0);
if(send(socketClient,sendBuf,strlen(sendBuf)+1,0)<=len)
{cout << "send successful" << endl;}
closesocket(socketClient);
}
可以编译通过,用的VC6.0,但是不能运行,不知道理由...Linking...
connect.obj : error LNK2001: unresolved external symbol __imp__closesocket@4
connect.obj : error LNK2001: unresolved external symbol __imp__send@16
connect.obj : error LNK2001: unresolved external symbol __imp__recv@16
connect.obj : error LNK2001: unresolved external symbol __imp__accept@12
connect.obj : error LNK2001: unresolved external symbol __imp__listen@8
connect.obj : error LNK2001: unresolved external symbol __imp__bind@12
connect.obj : error LNK2001: unresolved external symbol __imp__htonl@4
connect.obj : error LNK2001: unresolved external symbol __imp__htons@4
connect.obj : error LNK2001: unresolved external symbol __imp__socket@12
connect.obj : error LNK2001: unresolved external symbol __imp__WSAStartup@8
Debug/connect.exe : fatal error LNK1120: 10 unresolved externals
Error executing link.exe.这个是出错的地方...
[此贴子已经被作者于2006-11-22 16:09:00编辑过]