废话少说,上代码!
/server.c/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char *argv[])
{
int socket_fd, file_fd;
struct sockaddr_in netaddr;
struct sockaddr_in fromaddr;
socklen_t len = sizeof(struct sockaddr);
memset(&netaddr, 0, len);
netaddr.sin_family = AF_INET;
netaddr.sin_port = htons(5000);
netaddr.sin_addr.s_addr = INADDR_ANY;
if((socket_fd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
{
perror("Fail to create socket");
return -1;
}
if (bind(socket_fd, (struct sockaddr*)&netaddr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
while (1) {
char buf[100] = {};
int ret = recvfrom(socket_fd, buf, 100, 0, (struct sockaddr *)&fromaddr, &len);
if (ret == -1) {
perror("recvfrom");
exit(1);
}
printf("buf=%s\n", buf);
if (fork() == 0)
break;
printf("next==============\n");
}
int sock2;
if((sock2 = socket(AF_INET,SOCK_DGRAM,0)) < 0)
{
perror("child Fail to create socket");
return -1;
}
if (sendto(sock2, "world", 5, 0, (struct sockaddr*)&fromaddr, len) == -1) {
perror("child sendto");
exit(1);
}
while (1) {
char buf[100] = {};
int ret = recvfrom(sock2, buf, 100, 0, (struct sockaddr *)&fromaddr, &len);
if (ret == -1) {
perror("child recvfrom");
exit(1);
}
printf("child buf=%s\n", buf);
}
close(socket_fd);
return 0;
}
/*client.c*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char *argv[])
{
int socket_fd, file_fd;
struct sockaddr_in netaddr;
struct sockaddr_in fromaddr;
struct sockaddr_in servaddr;
socklen_t len = sizeof(struct sockaddr);
memset(&netaddr, 0, len);
netaddr.sin_family = AF_INET;
netaddr.sin_port = htons(0);
netaddr.sin_addr.s_addr = INADDR_ANY;
if((socket_fd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
{
perror("Fail to create socket");
return -1;
}
if (bind(socket_fd, (struct sockaddr*)&netaddr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
memset(&servaddr, 0, len);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(5000);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");;
if (sendto(socket_fd, "hello", 5, 0, (struct sockaddr*)&servaddr, len) == -1) {
perror("sendto");
exit(1);
}
char buf[100] = {};
if (recvfrom(socket_fd, buf, 100, 0, (struct sockaddr*)&fromaddr, &len) == -1) {
perror("recvfrom");
exit(1);
}
printf("recvfrom ip %s : %d\n", inet_ntoa(fromaddr.sin_addr), ntohs(fromaddr.sin_port));
printf("buf=%s\n", buf);
int i;
for (i = 0; i < 10; i++) {
char buf[100];
int ret = sprintf(buf, "shit %d", i);
if (sendto(socket_fd, buf, ret, 0, (struct sockaddr*)&fromaddr, len) == -1) {
perror("sendto");
exit(1);
}
printf("send ok\n");
sleep(1);
}
//sleep(5);
close(socket_fd);
return 0;
}
阅读(1079) | 评论(0) | 转发(0) |