gcc 编译:
gcc -o server server.c -lws2_32
gcc -o client client.c -lws2_32
返回代码,查询地址
server端代码
-
#include <stdio.h>
-
#include <Winsock2.h>
-
-
WORD RPort =6666; //客户端 port
-
char RIP[16]="127.0.0.1";
-
WORD LPort =7777; //服务端 port
-
char LIP[16]="127.0.0.1";
-
-
SOCKET S;
-
struct sockaddr_in rAddr;
-
struct sockaddr_in lAddr;
-
-
WSADATA WD; //winsock dll 消息
-
int r ; // result
-
-
void ShowInfo(char *info)
-
{
-
puts(info);
-
exit(1);
-
}
-
void SetSockAddr(struct sockaddr_in *A,WORD Port,char *IP)
-
{
-
A->sin_family=AF_INET; //tcp/tp协议 win下PF_INET 与AF_INET 相同
-
A->sin_port=htons(Port); //端口 主机字节顺序转网络字节顺序
-
A->sin_addr.s_addr=inet_addr(IP); //IP地址
-
}
-
int main(int argc,char *argv[])
-
{
-
-
WORD V;
-
V=0x0202 ; //winsock 版本 2.2 ,可以用 MAKEWORD(2,2) 表示
-
-
r=WSAStartup(V,(LPWSADATA)&WD); //载入适合的winsock动态链接库
-
if(r == SOCKET_ERROR) ShowInfo("Start Error"); // SOCKET_ERROR 等于 -1
-
-
S=socket(AF_INET,SOCK_DGRAM,0); // create win socket
-
//socket(int af,int type,int protocel)
-
-
//程序结束前使用closesocket(S)
-
if(S == SOCKET_ERROR) ShowInfo("socket create error");
-
-
SetSockAddr(&lAddr,LPort,LIP);
-
-
r=bind(S,(struct sockaddr far *)&lAddr,sizeof(lAddr));
-
if(r == SOCKET_ERROR) ShowInfo("bind error");
-
-
SetSockAddr(&rAddr,RPort,RIP);
-
-
char msg[80];
-
-
do{
-
puts("wait client....");
-
-
//接收client端信息
-
r=recv(S,msg,80,0);
-
if(r ==SOCKET_ERROR ) ShowInfo("receive error");
-
-
msg[r]='\0';
-
-
-
if(!strcmp(msg,"exit")) {
-
printf("client is exit\n");
-
break;
-
}
-
-
puts("client message :");
-
puts(msg);
-
-
// inform client message is succeed receive
-
strcpy(msg,"ok!");
-
r=sendto(S,msg,strlen(msg),0,(struct sockaddr far *)&rAddr,sizeof(rAddr));
-
if( r == SOCKET_ERROR ) ShowInfo("send error");
-
-
} while(1);
-
closesocket(S);
-
WSACleanup();
-
-
return 0;
-
}
client端代码
-
#include <stdio.h>
-
#include <winsock2.h>
-
-
WORD rport=7777;
-
char rip[16]="127.0.0.1";
-
WORD lport=6666;
-
char lip[16]="127.0.0.1";
-
-
SOCKET S;
-
-
struct sockaddr_in raddr;
-
struct sockaddr_in laddr;
-
-
WSADATA WD;
-
-
int r;
-
-
void ShowInfo(char *info){
-
puts(info);
-
printf("error code is %d\n",WSAGetLastError());
-
exit(1);
-
}
-
-
void SetSockAddr(struct sockaddr_in *A,WORD Port ,char *IP)
-
{
-
A->sin_family =AF_INET;
-
A->sin_port = htons(Port);
-
A->sin_addr.s_addr=inet_addr(IP);
-
-
}
-
-
int main(int argc ,char *argv[])
-
{
-
// load winsock dll
-
r = WSAStartup(MAKEWORD(2,2),&WD);
-
if(r == SOCKET_ERROR) ShowInfo(" start error");
-
-
//create win sokcet
-
S = socket(AF_INET,SOCK_DGRAM,0);
-
if(S == SOCKET_ERROR) ShowInfo( "socket error");
-
-
//message
-
char msg[80];
-
-
//set socket localhost
-
SetSockAddr(&laddr,lport,lip);
-
-
//指定 Socket 的 Local 位址
-
r=bind(S,(struct sockaddr far *)&laddr,sizeof(laddr));
-
if(r == SOCKET_ERROR ) ShowInfo(" bind error");
-
-
//set socket server
-
SetSockAddr(&raddr,rport,rip);
-
-
//--------get message and send messge
-
do{
-
puts("send:");
-
gets(msg);
-
-
//send to server
-
r=sendto(S,msg,strlen(msg),0,(struct sockaddr far *)&raddr,sizeof(raddr));
-
if( r == SOCKET_ERROR ) ShowInfo("send error");
-
-
// exit
-
if(! strcmp( msg,"exit")) break;
-
-
puts("send ok! wait server recieve");
-
-
//get recieve
-
r=recv(S,msg,80,0);
-
if( r== SOCKET_ERROR ) ShowInfo("recieve error");
-
puts("server:");
-
msg[r]='\0';
-
puts(msg);
-
-
-
}while(1);
-
-
closesocket(S); //close socket
-
WSACleanup(); //中止Windows Sockets DLL的使用
-
-
return 0;
-
}
转载自:
阅读(4162) | 评论(0) | 转发(0) |