一、通信流程
TCP:
UDP:
部分函数知识可以参考之前写的 Linux 网络编程——TCP
TCP通信:
客户端:
-
#include<stdio.h>
-
#include<sys/socket.h>
-
#include<netinet/in.h>
-
#include<stdlib.h>
-
#include<memory.h>
-
#include<string.h>
-
-
-
#define PORT 8888
-
int main(int argc,char **argv){
-
int sock;
-
struct sockaddr_in my_addr;
-
int len;
-
char buf[100];
-
if(argc <3){
-
printf("Usage: %s <ip> <port> <message>",argv[0]);
-
exit(1);
-
}
-
-
if((sock=socket(AF_INET,SOCK_STREAM,0))<0){
-
printf("socket create error!\n");
-
exit(1);
-
}
-
my_addr.sin_family=AF_INET;
-
my_addr.sin_port=htons(8888);
-
if(inet_aton(argv[1],(struct in_addr *)&my_addr.sin_addr.s_addr) == 0){
-
printf("%s chage error!\n",argv[1]);
-
exit(1);
-
}
-
if(connect(sock,(struct sockaddr*)&my_addr,sizeof(struct sockaddr))<0){
-
printf("connect error!\n");
-
exit(1);
-
}
-
printf("connected!\n");
-
-
while(1){
-
memset(buf,0,100);
-
len=recv(sock,buf,100,0);
-
if(len<0){
-
printf("recv error!\n");
-
exit(1);
-
}else if(len == 0){
-
printf("the client quit!\n");
-
exit(1);
-
}else{
-
printf("receive message: %s \n",buf);
-
}
-
printf("-------------------------\n");
-
memset(buf,0,100);
-
printf("Input message to send: ");
-
fgets(buf,100,stdin);
-
len=send(sock,buf,strlen(buf)-1,0);
-
if(len<0){
-
printf("send error!\n");
-
exit(1);
-
}else{
-
printf("send succeed! send : %s\n",buf);
-
}
-
}
-
close(sock);
-
}
服务端:
-
#include<stdio.h>
-
#include<sys/socket.h>
-
#include<netinet/in.h>
-
#include<stdlib.h>
-
#include<memory.h>
-
#include<string.h>
-
#include<sys/socket.h>
-
-
#define PORT 8888
-
int main(){
-
int sock,new_sock;
-
struct sockaddr_in my_addr,client_addr;
-
int len;
-
char buf[100];
-
-
if((sock=socket(AF_INET,SOCK_STREAM,0))<0){
-
printf("socket create error!\n");
-
exit(1);
-
}
-
memset(&my_addr,0,sizeof(my_addr));
-
my_addr.sin_family=AF_INET;
-
my_addr.sin_port=htons(PORT);
-
my_addr.sin_addr.s_addr=INADDR_ANY;
-
-
if(bind(sock,(struct sockaddr*)&my_addr,sizeof(my_addr))==-1){
-
printf("bind error!\n");
-
exit(1);
-
}
-
if(listen(sock,5)<0){
-
printf("listen error!\n");
-
exit(1);
-
}
-
len=sizeof(struct sockaddr);
-
if((new_sock=accept(sock,(struct sockaddr*)&client_addr,&len))<0){
-
printf("accept error!\n");
-
exit(1);
-
}else{
-
printf("server: get connection from %s,port %d socket %d \n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port),new_sock);
-
-
}
-
while(1){
-
memset(buf,0,100);
-
printf("Input message to send: ");
-
fgets(buf,100,stdin);
-
if(!strncasecmp(buf,"quit",4)){
-
printf("server is closing...\n");
-
break;
-
}
-
len=send(new_sock,buf,strlen(buf)-1,0);
-
if(len<0){
-
printf("send error!\n");
-
exit(1);
-
}else{
-
printf("send succeed! send : %s\n",buf);
-
}
-
printf("------------------------------\n");
-
memset(buf,0,100);
-
len=recv(new_sock,buf,100,0);
-
if(len<0){
-
printf("recv error!\n");
-
exit(1);
-
}else if(len == 0){
-
printf("the client quit!\n");
-
break;
-
}else{
-
printf("receive message: %s \n",buf);
-
}
-
}
-
close(sock);
-
close(new_sock);
-
}
-
相对于TCP来说UDP就要简单写,毕竟步骤少嘛:
服务端 :
-
#include <stdio.h>
-
#include <string.h>
-
#include <sys/types.h>
-
#include <netinet/in.h>
-
#include <sys/socket.h>
-
#include <errno.h>
-
#include <stdlib.h>
-
#include <arpa/inet.h>
-
-
int main(int argc, char **argv){
-
struct sockaddr_in s_addr,c_addr;
-
int sock;
-
socklen_t addr_len;
-
int len;
-
char buff[128];
-
-
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
-
perror("socket");
-
exit(errno);
-
} else
-
printf("create socket.\n\r");
-
-
memset(&s_addr, 0, sizeof(struct sockaddr_in));
-
-
s_addr.sin_family = AF_INET;
-
s_addr.sin_port = htons(7838);
-
s_addr.sin_addr.s_addr = INADDR_ANY;
-
-
if ((bind(sock, (struct sockaddr *) &s_addr, sizeof(s_addr))) == -1) {
-
perror("bind");
-
exit(errno);
-
} else
-
printf("bind address to socket.\n\r");
-
-
addr_len = sizeof(c_addr);
-
while (1) {
-
memset(buff,0,sizeof(buff));
-
-
len = recvfrom(sock, buff, sizeof(buff) - 1, 0, (struct sockaddr *) &c_addr, &addr_len);
-
if (len < 0) {
-
perror("recvfrom");
-
exit(errno);
-
}
-
-
buff[len] = '\0';
-
printf("recive come from %s:%d message:%s\n\r",inet_ntoa(c_addr.sin_addr), ntohs(c_addr.sin_port), buff);
-
}
-
return 0;
-
}
客户端:
-
#include <stdio.h>
-
#include <string.h>
-
#include <sys/types.h>
-
#include <netinet/in.h>
-
#include <sys/socket.h>
-
#include <errno.h>
-
#include <stdlib.h>
-
#include <arpa/inet.h>
-
-
int main(int argc, char **argv){
-
struct sockaddr_in s_addr;
-
int sock;
-
int addr_len;
-
int len;
-
char buff[128];
-
-
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
-
perror("socket");
-
exit(errno);
-
} else
-
printf("create socket.\n\r");
-
-
s_addr.sin_family = AF_INET;
-
s_addr.sin_port = htons(7838);
-
if (argc>2){
-
s_addr.sin_addr.s_addr = inet_addr(argv[1]);
-
strcpy(buff,argv[2]);
-
}
-
else {
-
printf("input sever ip!\n");
-
exit(0);
-
}
-
-
addr_len = sizeof(s_addr);
-
len = sendto(sock, buff, strlen(buff), 0, (struct sockaddr *) &s_addr, addr_len);
-
if (len < 0) {
-
printf("\n\rsend error.\n\r");
-
return 3;
-
}
-
-
printf("send success.\n\r");
-
return 0;
-
}
阅读(1191) | 评论(0) | 转发(0) |