我们从一个面试题说起,面试题目是这样的:socket网络编程的一般步骤是怎么样的?下面是参考答案:
对于TCP连接:
1.服务器端1)创建套接字socket;2)绑定端口号bind;3)监听连接listen;4)接受连接请求accept,并返回新的套接字;5)用新返回的套接字read/write;6)关闭套接字close。
2.客户端1)创建套接字socket; 2)发起建立连接请求connect; 3)发送/接收数据read/write;4)关闭套接字close。
TCP总结:
Server端:socket -- bind -- listen-- accept-- read/write-- close
Client端:socket------- conncet------read/write------close.
对于UDP连接:
1.服务器端:1)创建套接字socket;2)绑定端口号bind;3)接收/发送消息recvfrom/sendto;4)关闭套接字。
2.客户端:1)创建套接字socket;2)发送/接收消息sendto/recvfrom;3)关闭套接字.
UDP总结:
Server端:socket----bind ----recvfrom/sendto----close
Client端:socket---- sendto/recvfrom----close.
下面先给出所有API,再一一介绍之:
#include
int socket(int family,int type,int protocol);
int connect(int sockfd,const struct sockaddr *seraddr,socklen_t addrlen);
int bind(int sockfd,const struct sockaddr *myaddr,socklen_t addrlen);
int listen(int sockfd,int backlog);
int accept(int sockfd,struct sockaddr *clientaddr,socklen_t *addrlen);
int close(int sockfd);
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen);
ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
//读写函数也可用于套接字,在基于tcp的socket编程中是经常用他们来代替send/recv
#include
ssize_t read(int fd,void *buf,size_t count);
ssize_t read(int fd,const void *buf,size_t sount);
#include
ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
ssize_t preadv(int fd, const struct iovec *iov, int iovcnt,off_t offset);
ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt,off_t offset);
1.socket函数
int socket(int family,int type,int protocol);
//返回值,成功返回非负描述符,失败返回-1 下面是eg:
//socket(AF_INET,SOCK_STREAM,0); //建立基于IPV4的TCP套接字
//socket(AF_INET,SOCK_DGRAM,0); //建立基于IPV4的UDP套接字
2.bind函数
int bind(int sockfd,const struct sockaddr *myaddr,socklen_t len);
//成功返回0,失败返回-1。
/*
//套接字函数通用地址结构sa_xxx,
struct sockaddr{ //16bytes
uint8_t sa_len; //8bits,1byte
sa_family_t sa_family; //8bits,1byte
char sa_data[14]; //14bytes
};
//IPV4地址结构sin_xxx
struct in_addr{
in_addr_t s_addr; //32bits,4bytes
};
struct sockaddr_in{ //16bytes
uint8_t sin_len; //8bits,1byte
sa_family_t sin_family; //8bits,1byte
in_port_t sin_port; //16bits,2bytes
struct in_addr sin_addr; //32bits,4bytes
char sin_zero[8]; //8bytes
};
eg:
struct sockaddr_in seraddr;
memset(&seraddr,0,sizeof(sockaddr_in));
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(22);
seraddr.sin_addr.s_addr = htonl(INADDR_ANY);
//inet_pton(AF_INET, argv[1], (struct in_addr *)&servaddr.sin_addr);
bind(sockfd,(struct sockaddr *)&seraddr,sizeof(struct sockaddr));
*/
一个TCP连接的简单例子:
#include "unp.h"
#include
int
main(int argc, char **argv)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
char buff[MAXLINE];
time_t ticks;
listenfd = Socket(AF_INET, SOCK_STREAM, 0);
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);
}
}
上面是服务器程序,服务器接受客户端的连接,并返回当前时间给客户端
#include "unp.h"
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 ");
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err_sys("socket error");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(13); /* daytime server */
if (inet_pton(AF_INET, argv[1], &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) == EOF)
err_sys("fputs error");
}
if (n < 0)
err_sys("read error");
exit(0);
}
上图是TCP客户端程序,他连接服务器,取得服务器返回的时间后输出到终端
我们再给一个基于UDP实现的简单例子:以下是服务器程序,他取得客户端发送的字符串,并把该字符串返回给客户端
#include "unp.h"
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);
}
}
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr, cliaddr;
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
Bind(sockfd, (SA *) &servaddr, sizeof(servaddr));
dg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr));
}
以下是UDP客户端程序。
#include "unp.h"
void dg_cli(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; /* null terminate */
Fputs(recvline, stdout);
}
}
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
if (argc != 2)
err_quit("usage: udpcli ");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
dg_cli(stdin, sockfd, (SA *) &servaddr, sizeof(servaddr));
exit(0);
}