c语言udp通信实例(linux)
来源:互联网 发布:比较好玩的软件 编辑:程序博客网 时间:2024/05/21 14:03
c语言udp通信实例(linux) 2012-02-07 09:14:28
分类: C/C++
- server
- ---------------
- #include <stdio.h> /* These are the usual header files */
- #include <string.h>
- #include <unistd.h> /* for close() */
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #define PORT 1234 /* Port that will be opened */
- #define MAXDATASIZE 100 /* Max number of bytes of data */
- main()
- {
- int sockfd; /* socket descriptors */
- struct sockaddr_in server; /* server's address information */
- struct sockaddr_in client; /* client's address information */
- socklen_t sin_size;
- int num;
- char recvmsg[MAXDATASIZE]; /* buffer for message */
- char sendmsg[MAXDATASIZE];
- char condition[] = "quit";
- /* Creating UDP socket */
- if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
- /* handle exception */
- perror("Creating socket failed.");
- exit(1);
- }
- bzero(&server,sizeof(server));
- server.sin_family=AF_INET;
- server.sin_port=htons(PORT);
- server.sin_addr.s_addr = htonl (INADDR_ANY);
- if (bind(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) {
- /* handle exception */
- perror("Bind error.");
- exit(1);
- }
- sin_size=sizeof(struct sockaddr_in);
- while (1) {
- num = recvfrom(sockfd,recvmsg,MAXDATASIZE,0,(struct sockaddr *)&client,&sin_size);
- if (num < 0){
- perror("recvfrom error\n");
- exit(1);
- }
- recvmsg[num] = '\0';
- printf("You got a message (%s) from %s\n",recvmsg,inet_ntoa(client.sin_addr) ); /* prints client's IP */
- if(strcmp(recvmsg,condition)==0) break;
- int i=0;
- for(i = 0 ; i < num ; i ++)
- {
- sendmsg[i] = recvmsg[num-1-i];
- }
- sendmsg[num]='\0';
- sendto(sockfd,sendmsg,strlen(sendmsg),0,(struct sockaddr *)&client,sin_size);
- }
- close(sockfd); /* close listenfd */
- }
- ---------------------------------------------------------
- client
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <strings.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h> /* netbd.h is needed for struct hostent =) */
- #define PORT 1234 /* Open Port on Remote Host */
- #define MAXDATASIZE 100 /* Max number of bytes of data */
- int main(int argc, char *argv[])
- {
- int fd, numbytes; /* files descriptors */
- char recvbuf[MAXDATASIZE]; /* buf will store received text */
- char sendbuf[MAXDATASIZE];
- struct hostent *he; /* structure that will get information about remote host */
- struct sockaddr_in server,client; /* server's address information */
- if (argc !=2) { /* this is used because our program will need two argument (IP address and a message */
- printf("Usage: %s <IP Address> \n",argv[0]);
- exit(1);
- }
- if ((he=gethostbyname(argv[1]))==NULL){ /* calls gethostbyname() */
- printf("gethostbyname() error\n");
- exit(1);
- }
- if ((fd=socket(AF_INET, SOCK_DGRAM, 0))==-1){ /* calls socket() */
- printf("socket() error\n");
- exit(1);
- }
- bzero(&server,sizeof(server));
- server.sin_family = AF_INET;
- server.sin_port = htons(PORT); /* htons() is needed again */
- server.sin_addr = *((struct in_addr *)he->h_addr); /*he->h_addr passes "*he"'s info to "h_addr" */
- socklen_t len;
- len=sizeof(struct sockaddr_in);
- while (1) {
- printf("input message:");
- fgets(sendbuf,40,stdin);
- sendto(fd,sendbuf,strlen(sendbuf),0,(struct sockaddr *)&server,len);
- if ((numbytes=recvfrom(fd,recvbuf,MAXDATASIZE,0,(struct sockaddr *)&server,&len)) == -1){ /* calls recvfrom() */
- printf("recvfrom() error\n");
- exit(1);
- }
- recvbuf[numbytes]='\0';
- printf("Server Return Reverse Message: %s\n",recvbuf); /* it prints server's welcome message */
- }
- close(fd); /* close fd */
- }
- --------------------------------------------------------------------------------
- ./client 127.0.0.1
- input:test
- c语言udp通信实例(linux)
- c语言udp通信实例(linux)
- udp通信(C 语言)
- linux下C语言UDP通信
- linux下c语言udp连接实例
- c#UDP通信实例复习
- linux的进程通信:信号量实例(C语言)
- linux UDP 通信使用实例
- Linux下C语言实现简单的udp通信
- Linux C语言 UDP
- Linux网络通信实例(TCP和UDP)
- C语言--利用UDP实现socket通信
- c 语言下 TCP/UDP 通信
- linux中c语言和php语言通信代码UDP&TCP
- c语言 Linux UDP传输
- C语言socket通信实例
- [Linux C编程]UDP网络通信
- udp通信简单实例
- vbox中设置Ubuntu与主机文件共享的方法
- Ubuntu更改文件夹权限及chmod详细用法
- 高效能人士的七个习惯1
- iFrame自适应高度(兼容IE、谷歌、火狐、搜狗、opera、safari)
- js抽奖程序(跑马灯效果)
- c语言udp通信实例(linux)
- jQuery为新增元素绑定事件
- vs2010生成Dll文件并引用dll(C#)
- jdk的安装-配置
- 高效能人士的七个习惯2
- jquery判断checkbox选中
- 题目49:Least Common Multiple
- 关于radio单选框无法实现单选的原因
- [Web Chart系列之一(续)]Web端图形绘制SVG,VML, HTML5 Canvas 简单实例