本文章代码根据《UNIX网络编程:卷1》修改而来,由于书中源码用于linux系统,题主在windows系统下进行模拟操作,作了部分修改。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fcntl.h>
#include <Windows.h>
#include <winsock.h>
#include <winsock2.h>
#include "my_err.h"
#define bzero(ptr,n) memset(ptr,0,n)
#define SA struct sockaddr
void err_quit(const char *, ...);
void err_sys(const char *, ...);
int main(int argc,char **argv)
{
int sockfd,n;
char recvline[MAXLINE + 1];
struct sockaddr_in servaddr;
if(argc != 2)
err_quit("usage: a.out <IPaddress>");
WSADATA wsa;
WORD wVersionRequested;
wVersionRequested = MAKEWORD(2,0);
if(WSAStartup(wVersionRequested,&wsa)!=0){
printf("Winsock Initialization failed.\n");
}
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
err_sys("socket error");
std::cout<<sockfd<<std::endl;
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(13);
servaddr.sin_addr.s_addr = inet_addr(argv[1]);
/* * int inet_pton(int af,const char *sr,void *dst); * af: 地址簇 * *sr: 来源地址 * *dst: 接收转换后的数据 */
// if(inet_ntop(AF_INET ,argv[1],&servaddr.sin_addr,sizeof(servaddr.sin_addr)) <= 0)
// err_quit("inet_pton error for %s",argv[1]);
if(connect(sockfd,(SA *) &servaddr,sizeof(servaddr)) < 0)
err_sys("connect error");
while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {
recvline[n] = 0; /* null terminate */
if (fputs(recvline, stdout)r_sys("fputs error");
}
if (n < 0)
err_sys("read error");
exit(0);
}
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fcntl.h>
#include <Windows.h>
#include <winsock.h>
#include <winsock2.h>
#include "my_err.h"
#define bzero(ptr,n) memset(ptr,0,n)
#define SA struct sockaddr
#define LISTENQ 1024 /* 2nd argument to listen() */
int main(int argc,char **argv)
{
int listenfd,connfd;
struct sockaddr_in servaddr;
char buff[MAXLINE];
time_t ticks;
WSADATA wsa;
WORD wVersionRequested;
wVersionRequested = MAKEWORD(2,0);
if(WSAStartup(wVersionRequested,&wsa)!=0){
printf("Winsock Initialization failed.\n");
}
listenfd = socket(AF_INET,SOCK_STREAM,0);
std::cout<<listenfd<<std::endl;
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(13); /* daytime server */
bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
listen(listenfd, LISTENQ);
for ( ; ; ) {
connfd = accept(listenfd, (SA *) NULL, NULL);
ticks = time(NULL);
snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
write(connfd, buff, strlen(buff));
//close(connfd);
}
}
my_err.h 代码块如下:
#include <errno.h> /* for definition of errno */
#include <stdarg.h> /* ISO C variable aruments */
#define MAXLINE 4096
static void err_doit(int, int, const char *, va_list);
/* * Nonfatal error related to a system call. * Print a message and return. */
void
err_ret(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
}
/* * Fatal error related to a system call. * Print a message and terminate. */
void
err_sys(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
exit(1);
}
/* * Fatal error unrelated to a system call. * Error code passed as explict parameter. * Print a message and terminate. */
void
err_exit(int error, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, error, fmt, ap);
va_end(ap);
exit(1);
}
/* * Fatal error related to a system call. * Print a message, dump core, and terminate. */
void
err_dump(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
abort(); /* dump core and terminate */
exit(1); /* shouldn't get here */
}
/* * Nonfatal error unrelated to a system call. * Print a message and return. */
void
err_msg(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
}
/* * Fatal error unrelated to a system call. * Print a message and terminate. */
void
err_quit(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
exit(1);
}
/* * Print a message and return to caller. * Caller specifies "errnoflag". */
static void
err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
char buf[MAXLINE];
vsnprintf(buf, MAXLINE, fmt, ap);
if (errnoflag)
snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",
strerror(error));
strcat(buf, "\n");
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(NULL); /* flushes all stdio output streams */
}
运行测试时,先运行服务器代码,再运行客户端代码,服务器会一直处于侦听状态,客户端连接127.0.0.1,connect成功,但是会read error,提示Bad file descriptor。
但是,把服务器和客户端中write()和read()函数相应地改成send()和recv()就可以成功进行接收数据。