server端
/**********************************************************/
* */
* program name:udpserver.c */
* Author: jimmy */
* Funtion:This program is just a demostrate program */
* to tell students how to use socket to */
* make a udp communation. */
* Date: 08-20-2009 */
* */
***********************************************************/
#include<stdlib.h>
#include<stdio.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define PORT 8900
int main(int argc,char** argv)
{
struct sockaddr_in server;
struct sockaddr_in client;
int port;
int opt;
int recvnum;
int sendnum;
int sock;
int len;
char send_buf[2048];
char recv_buf[2048];
//if (2==argc)
port= PORT;
memset(send_buf,0,2048);
memset(recv_buf,0,2048);
opt = SO_REUSEADDR;
if (-1==(sock=socket(AF_INET,SOCK_DGRAM,0)))
{
perror("create listen socket error/n");
exit(1);
}
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
#ifdef DEBUG
printf("the listen id is %d/n",listend);
#endif
memset(&server,0,sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(port);
if (-1==bind(sock,(struct sockaddr *)&server,sizeof(struct sockaddr)))
{
perror("bind error/n");
exit(1);
}
len = sizeof(struct sockaddr_in);
while(1)
{
memset(recv_buf,0,sizeof(recv_buf));
if(0>(recvnum = recvfrom(sock,recv_buf,2048,0,(struct sockaddr *)&client,&len)))
{
printf("receive data error");
close(sock);
exit(1);
}
recv_buf[recvnum] = '/0';
printf("receive from client :%s/n",recv_buf);
printf("the client ip is %s/n",inet_ntoa(client.sin_addr));
sendnum = sprintf(send_buf,"hello the guest form %s/nbyebye,the guest from %s/n",inet_ntoa(client.sin_addr),inet_ntoa(client.sin_addr));
if(0>(sendnum = sendto(sock,send_buf,sendnum,0,(struct sockaddr *)&client,sizeof(struct sockaddr_in))))
{
printf("send data error");
close(sock);
exit(1);
}
}
close(sock);
return 0;
}
client端:
/**********************************************************/
* */
* program name:udpclient.c */
* Author: jimmy */
* Funtion:This program is just a demostrate program */
* to tell students how to use socket to */
* make a udp communation. */
* Date: 08-20-2009 */
* */
***********************************************************/
#include<stdlib.h>
#include<stdio.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define PORT 8900
void print_usage(char * cmd)
{
fprintf(stderr," %s usage:/n",cmd);
fprintf(stderr,"%s IP_Addr [port]/n",cmd);
}
int main(int argc,char** argv)
{
struct sockaddr_in server;
int ret;
int len;
int port;
int sockfd;
int sendnum;
int recvnum;
char send_buf[2048];
char recv_buf[2048];
if ((2>argc)|| (argc >3))
{
print_usage(argv[0]);
exit(1);
}
if (3==argc)
{
port = atoi(argv[2]);
}
if (-1==(sockfd=socket(AF_INET,SOCK_DGRAM,0)))
{
perror("can not create socket/n");
exit(1);
}
memset(&server,0,sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(argv[1]);
server.sin_port = htons(port);
printf("what words do you want to tell to server:/n");
fgets(send_buf,1024,stdin);
#ifdef DEBUG
printf("%s/n",send_buf);
#endif
if (0>(len=sendto(sockfd,send_buf,strlen(send_buf),0,(struct sockaddr *)&server,sizeof(struct sockaddr))))
{
perror("send data error/n");
close(sockfd);
exit(1);
}
len = sizeof(struct sockaddr);
if (0>(len=recvfrom(sockfd,recv_buf,2048,0,(struct sockaddr *)&server,&len)))
{
perror("recv data error/n");
close(sockfd);
exit(1);
}
recv_buf[len]='/0';
printf("the message from the server is:%s/n",recv_buf);
close(sockfd);
}