一.UDP
服务器端代码如下
#include
#include
#include
#include
#include
#include
int main(int argc,char *argv[])
{
if(argc!=3){
printf("Usage: %s [ip] [port]\n",argv[0]);
return 1;
}
int sock=socket(AF_INET,SOCK_DGRAM,0);
if(sock<0){
perror("socket");
return 2;
}
struct sockaddr_in local;
local.sin_family=AF_INET;
local.sin_port=htons(atoi(argv[2]));
local.sin_addr.s_addr=inet_addr(argv[1]);//192.168.43.243
if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0){
perror("bind");
return 3;
}
char buf[1024];
while(1){
struct sockaddr_in client;
socklen_t len =sizeof(client);
ssize_t s=recvfrom(sock,buf,sizeof(buf)-1,0,\
(struct sockaddr*)&client,&len);
if(s>0){
buf[s]=0;
printf("[%s:%d]: %s",inet_ntoa(client.sin_addr),\
ntohs(client.sin_port),buf);
sendto(sock,buf,strlen(buf),0,\
(struct sockaddr*)&client,sizeof(client));
}
}
return 0;
}
客户端代码如下
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char *argv[])
{
if(argc != 3){
printf("Usage: %s [server:ip] [server:port]\n",argv[0]);
return 1;
}
int sock=socket(AF_INET,SOCK_DGRAM,0);
if(sock<0){
perror("sock");
return 2;
}
char buf[1024];
while(1){
struct sockaddr_in server;
server.sin_family=AF_INET;
server.sin_port=htons(atoi(argv[2]));
server.sin_addr.s_addr=inet_addr(argv[1]);
printf("please enter:");
ssize_t s=read(0,buf,sizeof(buf)-1);
if(s>0){
buf[s]=0;
sendto(sock,buf,strlen(buf),0,(struct sockaddr*)&server,sizeof(server));
recvfrom(sock,buf,sizeof(buf)-1,0,NULL,NULL);
buf[s]=0;
sendto(sock,buf,strlen(buf),0,(struct sockaddr*)&server,sizeof(server));
recvfrom(sock,buf,sizeof(buf)-1,0,NULL,NULL);
printf("server echo# %s\n",buf);
}
}
return 0;
}
运行结果如下
二.TCP
服务器端代码如下
#include
#include
#include
#include
#include
#include
#include
void serviceIO(int sock)
{
char buf[1024];
while(1){
ssize_t s= read(sock,buf,sizeof(buf)-1);
if(s>0){
buf[s]=0;
printf("client# %s\n",buf);
}
else if(s==0){
printf("client quit\n");
break;
}
else{
perror("read");
break;
}
}
close(sock);
}
int main(int argc,char *argv[])
{
if(argc != 3){
printf("Usage: %s [ip] [port]\n",argv[0]);
return 1;
}
int listen_sock=socket(AF_INET,SOCK_STREAM,0);
if(listen_sock<0){
perror("sock");
return 2;
}
struct sockaddr_in local;
local.sin_family=AF_INET;
local.sin_port=htons(atoi(argv[2]));
local.sin_addr.s_addr=inet_addr(argv[1]);
if(bind(listen_sock,(struct sockaddr*)&local,sizeof(local))){
perror("bind");
return 3;
}
int s=listen(listen_sock,5);
if(s<0){
perror("listen");
return 4;
}
for( ; ;){
struct sockaddr_in client;
socklen_t len=sizeof(client);
int sock=accept(listen_sock,(struct sockaddr*)&client,&len);
if(sock<0){
perror("accept");
continue;
}
printf("get a new link [%s:%d]...!\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));
serviceIO(sock);
}
return 0;
}
客户端代码如下
#include
#include
#include
#include
#include
#include
int main(int argc,char *argv[])
{
if(argc != 3){
printf("Usage: %s [server.ip] [server.port]\n",argv[0]);
return 1;
}
int sock=socket(AF_INET,SOCK_STREAM,0);
if(sock<0){
perror("socket");
return 2;
}
struct sockaddr_in server;
server.sin_family=AF_INET;
server.sin_port=htons(atoi(argv[2]));
server.sin_addr.s_addr=inet_addr(argv[1]);
if(connect(sock,(struct sockaddr*)&server,sizeof(server))<0){
perror("connect");
return 3;
}
char buf[1024];
while(1){
printf("please enter:");
ssize_t s=read(0,buf,sizeof(buf)-1);
if(s>0){
buf[s]=0;
write(sock,buf,strlen(buf));
}
}
return 0;
}
运行结果如下