#include
#include
#include
#include
#include
#include
#define BUFSIZE 128
int main(void)
{
int sd;
struct sockaddr_in my_end, his_end;
socklen_t his_len;
int ret;
char buf[BUFSIZE];
sd = socket(PF_INET, SOCK_DGRAM, 0);
if (sd == -1) {
perror("socket()");
goto socket_err;
}
my_end.sin_family = AF_INET;
my_end.sin_port = htons(8899);
my_end.sin_addr.s_addr = INADDR_ANY;
ret = bind(sd, (struct sockaddr *)&my_end, sizeof(my_end));
if (ret == -1) {
perror("bind()");
goto bind_err;
}
his_len = sizeof(his_end);
while (1) {
ret = recvfrom(sd, buf, BUFSIZE, 0, (struct sockaddr *)&his_end, &his_len);
write(1, buf, ret);
ret = snprintf(buf, BUFSIZE, "send form: %d", getpid());
sendto(sd, buf, ret, 0, (struct sockaddr *)&his_end, his_len);
}
close(sd);
return 0;
bind_err:
close(sd);
socket_err:
return 1;
}
#include
#include
#include
#include
#include
#include
#include
#define BUFSIZE 128
int main(void)
{
int sd;
struct sockaddr_in my_end, his_end;
socklen_t his_len;
int ret;
char buf[BUFSIZE];
char *retp;
sd = socket(PF_INET, SOCK_DGRAM, 0);
if (sd == -1) {
perror("socket()");
goto socket_err;
}
his_end.sin_family = AF_INET;
his_end.sin_port = htons(8899);
inet_pton(AF_INET, "192.168.1.254", &his_end.sin_addr);
his_len = sizeof(his_end);
while (1) {
retp = fgets(buf, BUFSIZE, stdin);
if (retp == NULL) {
break;
}
sendto(sd, buf, strlen(buf), 0, (struct sockaddr *)&his_end, his_len);
ret = recvfrom(sd, buf, BUFSIZE, 0, (struct sockaddr *)&his_end, &his_len);
write(1, buf, ret);
}
close(sd);
return 0;
bind_err:
close(sd);
socket_err:
return 1;
}
阅读(1075) | 评论(0) | 转发(0) |