int connect( SOCKET s, const struct sockaddr FAR *name, int namelen );
第一个参数是一个套接字,第二个参数是一个地址结构体的指针,第三个参数是地址指针的长度。
// TCP client
#include <Winsock2.h>
#include <stdio.h>
void main()
{
//load socket lib. Use WSAStartup()
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
WSACleanup( );
return;
}
/* The WinSock DLL is acceptable. Proceed. */
SOCKET sockClient = socket(AF_INET,SOCK_STREAM,0);
//connect();
SOCKADDR_IN addrSrv; //绑定到服务器的socket上面去
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSrv.sin_family = AF_INET; //这个地方不需要网络字节序,加了htonl会出错
addrSrv.sin_port=htons(6000);
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
char recvBuf[100]={'0'};
//recvBuf[100] = '\0';
recv(sockClient,recvBuf,100,0);
printf("%s\n",recvBuf);
send(sockClient,"This is zhangsan",strlen("This is zhangsan")+1,0);
closesocket(sockClient);
WSACleanup();
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
UDP 的sock通信
//UDPClient 记得要link ws2_32.lib库
#include <stdio.h>
#include <Winsock2.h>
void main(){
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}
SOCKET sockClent = socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);
int len = sizeof(SOCKADDR);
char recvBuf[100];
memset(recvBuf,0,100);
recvfrom(sockClent,recvBuf,100,0,(SOCKADDR*)&addrSrv,(int*)sizeof(SOCKADDR));
printf("%s",recvBuf);
sendto(sockClent,"hello server",strlen("hello server")+1,0,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
closesocket(sockClent);
WSACleanup();
}
/
//UDP server
#include <stdio.h>
#include <Winsock2.h>
void main(){
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}
SOCKET sockSrv = socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
printf("socket listening...\n");
//listen(sockSrv,5);
SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR);//这个地方的sizeof中的参数
//SOCKET sockConn =accept(sockSrv,(SOCKADDR*)&addrClient,&len);
char recvBuf[100];
// memset((void*)recvBuf,0,100);
int returnValue;
returnValue= recvfrom(sockSrv,recvBuf,100,0,(SOCKADDR*)&addrClient,&len);
printf("%s\n",recvBuf);
if (returnValue == 0)
{
sendto(sockSrv,"hello client",sizeof("hello client")+1,0,(SOCKADDR*)&addrClient,len);
}
closesocket(sockSrv);
WSACleanup();
}