1.服务端代码
[cpp] view plain copy print ?
- #include <sys/types.h> /* See NOTES */
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <strings.h>
- #include <string.h>
- #include <errno.h>
-
- #define SERV_PORT 8000
- #define MAXLINE 1024
- #define SA struct sockaddr
-
- void dg_echo(int sockfd, SA* pcliaddr, socklen_t clilen);
-
- int main(int argc, char* argv[])
- {
- int sockfd, ret;
- struct sockaddr_in servaddr, cliaddr;
- sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- if (sockfd < 0)
- {
- printf("socket error, %d:%s\n", errno, strerror(errno));
- return -1;
- }
- bzero(&servaddr, sizeof(servaddr));
-
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
- servaddr.sin_port = htons(SERV_PORT);
-
- ret = bind(sockfd, (SA*)&servaddr, sizeof(servaddr));
- if (ret < 0)
- {
- printf("bind error, %d:%s\n", errno, strerror(errno));
- return -1;
- }
- dg_echo(sockfd, (SA*)&cliaddr, sizeof(cliaddr));
-
- return 0;
- }
-
- void dg_echo(int sockfd, SA* pcliaddr, socklen_t clilen)
- {
- int n;
- socklen_t len;
- char mesg[MAXLINE];
-
- for (;;)
- {
- len = clilen;
- n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
- sendto(sockfd, mesg, n, 0, pcliaddr, len);
- }
- }
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <errno.h>
#define SERV_PORT 8000
#define MAXLINE 1024
#define SA struct sockaddr
void dg_echo(int sockfd, SA* pcliaddr, socklen_t clilen);
int main(int argc, char* argv[])
{
int sockfd, ret;
struct sockaddr_in servaddr, cliaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
printf("socket error, %d:%s\n", errno, strerror(errno));
return -1;
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
ret = bind(sockfd, (SA*)&servaddr, sizeof(servaddr));
if (ret < 0)
{
printf("bind error, %d:%s\n", errno, strerror(errno));
return -1;
}
dg_echo(sockfd, (SA*)&cliaddr, sizeof(cliaddr));
return 0;
}
void dg_echo(int sockfd, SA* pcliaddr, socklen_t clilen)
{
int n;
socklen_t len;
char mesg[MAXLINE];
for (;;)
{
len = clilen;
n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
sendto(sockfd, mesg, n, 0, pcliaddr, len);
}
}
2.客户端代码
[cpp] view plain copy print ?
- #include <sys/types.h> /* See NOTES */
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <strings.h>
- #include <string.h>
-
- #define SERV_PORT 8000
- #define MAXLINE 1024
- #define SA struct sockaddr
-
- void dg_echo(FILE*fp, int sockfd, const SA* pservaddr, socklen_t servlen);
-
- int main(int argc, char* argv[])
- {
- int sockfd;
- struct sockaddr_in servaddr;
- bzero(&servaddr, sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_port = htons(SERV_PORT);
- inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
-
- sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- if (sockfd < 0)
- {
- printf("socket error, %d:%s\n", errno, strerror(errno));
- return -1;
- }
-
- dg_echo(stdin, sockfd, (SA*)&servaddr, sizeof(servaddr));
-
- return 0;
- }
-
- void dg_echo(FILE* fp, int sockfd, const SA* pservaddr, socklen_t servlen)
- {
- int n;
- char sendline[MAXLINE], recvline[MAXLINE + 1];
- while(fgets(sendline, MAXLINE, fp) != NULL)
- {
- sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
- n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
- recvline[n] = 0;
- fputs(recvline, stdout);
- }
- }
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#define SERV_PORT 8000
#define MAXLINE 1024
#define SA struct sockaddr
void dg_echo(FILE*fp, int sockfd, const SA* pservaddr, socklen_t servlen);
int main(int argc, char* argv[])
{
int sockfd;
struct sockaddr_in servaddr;
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
printf("socket error, %d:%s\n", errno, strerror(errno));
return -1;
}
dg_echo(stdin, sockfd, (SA*)&servaddr, sizeof(servaddr));
return 0;
}
void dg_echo(FILE* fp, int sockfd, const SA* pservaddr, socklen_t servlen)
{
int n;
char sendline[MAXLINE], recvline[MAXLINE + 1];
while(fgets(sendline, MAXLINE, fp) != NULL)
{
sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
recvline[n] = 0;
fputs(recvline, stdout);
}
}