基于TCP(面向连接)的socket编程,分为客户端和服务器端。
客户端的流程如下:
(1)创建套接字(socket)
(2)向服务器发出连接请求(connect)
(3)和服务器端进行通信(send/recv)
(4)关闭套接字
服务器端的流程如下:
(1)创建套接字(socket)
(2)将套接字绑定到一个本地地址和端口上(bind)
(3)将套接字设为监听模式,准备接收客户端请求(listen)
(4)等待客户请求到来;当请求到来后,接受连接请求,返回一个新的对应于此次连接的套接字(accept)
(5)用返回的套接字和客户端进行通信(send/recv)
(6)返回,等待另一个客户请求。
(7)关闭套接字。
服务端(server)
/*************************************************************************
> File Name: tcpsever.c
> Author: kid
> Mail: 280197326@qq.com
> Created Time: 2014年03月01日 星期六 23时25分29秒
************************************************************************/
#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>
#define MYPORT 3490 /* 监听端口 */
#define BACKLOG 10 /* listen的请求接收队列长度 */
void main()
{
int sockfd, new_fd; /* 监听端口,数据端口 */
struct sockaddr_in my_addr; /* 自身的地址信息 */
struct sockaddr_in their_addr; /* 连接对方的地址信息 */
int sin_size;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT); /* 网络字节顺序 */
my_addr.sin_addr.s_addr = INADDR_ANY; /* 自动填本机IP */
bzero(&(my_addr.sin_zero), 8); /* 其余部分置0 */
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) { /* 主循环 */
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1)
{
perror("accept");
continue;
}
printf("Got connection from %s\ n", inet_ntoa(their_addr.sin_addr));
if (!fork()) { /* 子进程 */
if (send(new_fd, "Hello, world!\ n", 14, 0) == -1) perror("send");
close(new_fd);
exit(0);
}
close(new_fd); /* 无法生成子进程时有用 */
while(waitpid(-1,NULL,WNOHANG) > 0); /*清除所有子进程 */
}
}
客户端(client)
/*************************************************************************
> File Name: tcpclient.c
> Author: kid
> Mail: 280197326@qq.com
> Created Time: 2014年03月01日 星期六 23时20分07秒
************************************************************************/
#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>
#define PORT 3490 /* Server的端口 */
#define MAXDATASIZE 100 /*一次可以读的最大字节数 */
int main(int argc, char *argv[])
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he; /* 主机信息 */
struct sockaddr_in their_addr; /* 对方地址信息 */
if (argc != 2) {
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
if ((he=gethostbyname(argv[1])) == NULL) {
/* get the host info */
herror("gethostbyname");
exit(1);
}
if ((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT); /* short, NBO */
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero), 8); /* 其余部分设成0 */
if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {
perror("connect");
exit(1);
}
if ((numbytes=recv(sockfd,buf,MAXDATASIZE,0))==-1) {
perror("recv");
exit(1);
}
buf[numbytes] = '\0';
printf("Received: %s",buf);
close(sockfd);
return 0;
}