注意:sockaddr地址结构体中的成员,除了sin_family之外,其他的成员都要使用网络字节序(高地址先存,低地址后存,而一般的基于inter的CPU都是低地址先存,高地址后存),所以我们要把这些成员改变为网络字节序(htonl(INADDR_ANY);相关的操作)
client和server均在本机。
服务器端代码:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SERVPORT 3333 /*服务器监听端口号 */
#define BACKLOG 10 /* 最大同时连接请求数 */
main()
{
int sockfd,client_fd; /*sock_fd:监听socket;client_fd:数据传输socket */
struct sockaddr_in my_addr; /* 本机地址信息 */
struct sockaddr_in remote_addr; /* 客户端地址信息 */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket创建出错!"); exit(1);
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr = INADDR_ANY; //zb: servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(my_addr.sin_zero),8);
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind出错!");
exit(1);
}
if (listen(sockfd, BACKLOG) == -1) {
perror("listen出错!");
exit(1);
}
while(1) {
socklen_t sin_size = sizeof(struct sockaddr_in);
if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1) {
perror("accept出错");
continue;
}
printf("received a connection from %s/n", inet_ntoa(remote_addr.sin_addr));
/***** zb add *****/
char str[INET_ADDRSTRLEN];
printf("received from %s at PORT %d/n",
inet_ntop(AF_INET, &remote_addr.sin_addr, str, sizeof(str)),
ntohs(remote_addr.sin_port));
/***** zb end *****/
if (!fork()) { /* 子进程代码段 */
//zb: while(1){
//write(client_fd, "Hello, you are connected!/n", strlen("Hello,... ed!/n"));
if (send(client_fd, "Hello, you are connected!/n", 26, 0) == -1)
perror("send出错!");
/***** zb add *****/
char bug[128] = {0};
//read(client_fd, bug, sizeof(bug)); if read == 0, means link break!
if (recv(client_fd, bug, sizeof(bug), 0) == -1)
perror("recv出错!");
printf("Received client : %s/n",bug);
/***** zb end *****/
// } /*end while*/
close(client_fd);
exit(0);
}
close(client_fd);
}
}
客户端代码:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#define SERVPORT 3333
#define MAXDATASIZE 100 /*每次最大数据传输量 */
main(int argc, char *argv[])
{
int sockfd, recvbytes;
char buf[MAXDATASIZE];
struct hostent *host;
struct sockaddr_in serv_addr;
if (argc < 2) {
fprintf(stderr,"Please enter the server's hostname!/n");
exit(1);
}
/* zb: make argv[1] = "localhost" */
if((host=gethostbyname(argv[1]))==NULL) {
herror("gethostbyname出错!");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror("socket创建出错!");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVPORT);
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
//zb: inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
// serv_addr.sin_addr.s_addr = inet_addr("192.168.0.1");
// inet_aton("127.0.0.1",&serv_addr.sin_addr);
bzero(&(serv_addr.sin_zero),8);
if (connect(sockfd, (struct sockaddr *)&serv_addr, /
sizeof(struct sockaddr)) == -1) {
perror("connect出错!");
exit(1);
}
//zb: while(1){
if ((recvbytes=recv(sockfd, buf, MAXDATASIZE, 0)) ==-1) {
perror("recv出错!");
exit(1);
}
buf[recvbytes] = '/0';
printf("Received: %s",buf);
/***** zb add *****/
if (send(sockfd, "Yes, I see, over !/n", 19, 0) == -1)
perror("send出错!");
/***** zb add *****/
// } /*end while*/
close(sockfd);
}