TCP——服务器程序设计
步骤
1)创建一个socket,用函数socket();
2)绑定IP地址、端口等信息到socket上,用函数bind();
3)设置允许的最大连接数,用函数listen();
4)等待来自客户端的连接请求,用函数accept();
5)收发数据,用函数send()和recv(),或者read()和write();
6)关闭网络连接;
TCP——客户端程序设计
步骤
1)创建一个socket,用函数socket();
2)设置要连接的服务器的IP地址和端口等属性;
3)连接服务器,用函数connect();
4)收发数据,用函数send()和recv(),或者read()和write();
5)关闭网络连接;
示例
tcp_server.c
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define portnumber 3333
int main(int argc, char *argv[])
{
int sockfd, new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size;
int nbytes;
char buffer[1024];
//创建socket
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(stderr, "Socket error:%s\n\a", strerror(errno));
exit(1);
}
//填充sockaddr结构
bzero(&server_addr, sizeof(struct sockaddr_in)); // 初始化,置0
server_addr.sin_family = AF_INET; // IPV4
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // IP地址转换为网络字节序格式
server_addr.sin_port = htons(portnumber); // 端口转换为网络字节序格式
//bind
if(bind(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1)
{
fprintf(stderr, "Bind error:%s\n\a", strerror(errno));
exit(1);
}
//listen
if(listen(sockfd, 5) == -1)
{
fprintf(stderr, "Listen error:%s\n\a", strerror(errno));
exit(1);
}
while(1)
{
//accept
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sockfd, (struct sockaddr *)(&client_addr), &sin_size)) == -1)
{
fprintf(stderr, "Accept error:%s\n\a", strerror(errno));
exit(1);
}
//fprintf(stderr, "Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));
//read
if((nbytes = read(new_fd, buffer, 1024)) == -1)
{
fprintf(stderr, "Read error:%s\n\a", strerror(errno));
exit(1);
}
buffer[nbytes] = '\0';
printf("Server received %s\n", buffer);
//close
close(new_fd);
}
//结束通讯
close(sockfd);
return 0;
}
gcc编译:
root@192 tcp]# gcc tcp_server.c -o tcp_server
[root@192 tcp]# ls
tcp_server tcp_server.c
[root@192 tcp]#
tcp_client.c
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdlib.h>
#include <netdb.h>
#define portnum 3333
int main(int argc, char *argv[])
{
int sockfd;
char buffer[128];
struct sockaddr_in server_addr;
struct hostent *host;
if(argc != 2)
{
fprintf(stderr, "Usage:%s hostname \a\n", argv[0]);
exit(1);
}
if((host = gethostbyname(argv[1])) == NULL)
{
fprintf(stderr, "Gethostname error\n");
exit(1);
}
//创建套接字
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
printf("creat socket error!\n");
exit(1);
}
//设置连接服务器地址
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(portnum);
server_addr.sin_addr = *((struct in_addr *)host->h_addr); //ip地址
//connect
if (connect(sockfd,(struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1)
{
printf("connect error! \n");
exit(1);
}
//send
printf("please input char:\n");
fgets(buffer, 128, stdin);
write(sockfd, buffer, strlen(buffer));
//close
close(sockfd);
return 0;
}
gcc编译:
[root@192 tcp]# gcc tcp_client.c -o tcp_client
[root@192 tcp]# ls
tcp_client tcp_client.c tcp_server tcp_server.c
[root@192 tcp]#
先后运行tcp_server和tcp_client即可看到结果。
|