1.参考链接
(1)【Linux网络编程】TCP网络编程中connect()、listen()和accept()三者之间的关系:
https://blog.csdn.net/dengjin20104042056/article/details/52357452
(2)Linux网络编程基础-03_TCP/IP网络编程
https://blog.csdn.net/Set_Mode/article/details/90229239
2.关键函数
(1)socket
NAME
socket - create an endpoint for communication
SYNOPSIS
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
(2)bind
NAME
bind - bind a name to a socket
SYNOPSIS
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
(3)listen
NAME
listen - listen for connections on a socket
SYNOPSIS
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int listen(int sockfd, int backlog);
(4)accept
NAME
accept, accept4 - accept a connection on a socket
SYNOPSIS
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <sys/socket.h>
int accept4(int sockfd, struct sockaddr *addr,
socklen_t *addrlen, int flags);
(5)read
NAME
read - read from a file descriptor
SYNOPSIS
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
(6)write
NAME
write - write to a file descriptor
SYNOPSIS
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
3.代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/types.h>
#include <arpa/inet.h>
int main(int argc,char *argv[])
{
int tcp_fd,ret,accept_fd,i;
char buf[1024];
char IPdotdec[20]="127.0.0.1";
struct in_addr addr_pton;
socklen_t client_addr_len;
struct sockaddr_in service_addr,client_addr;
service_addr.sin_family=AF_INET;
service_addr.sin_port=htons(9527);
service_addr.sin_addr.s_addr=htonl(INADDR_ANY);
//memset(service_addr,sizeof(service_addr),0);
//1.socket
tcp_fd=socket(AF_INET,SOCK_STREAM,0);
if(tcp_fd<0)
{
perror("error socket!\n");
exit(1);
}
//2.bind
ret=bind(tcp_fd,(struct sockaddr*)&service_addr,sizeof(service_addr));
if(ret!=0)
{
perror("error bind!\n");
exit(1);
}
//3.listen
listen(tcp_fd,128);
//4.accept
client_addr_len=sizeof(client_addr);
accept_fd=accept(tcp_fd,(struct sockaddr*)&client_addr,&client_addr_len);
if(accept_fd<0)
{
perror("error accept!\n");
exit(1);
}
//5.read & write
int count=5;
while(1)
{
ret=read(accept_fd,buf,1024);
if(ret>0)
{
printf("client_data_in:%s\n",buf);
//write(STDOUT_FILENO,buf,ret);
for(i=0;i<ret;i++)
for(i=0;i<ret;i++)
{
buf[i]=toupper(buf[i]);
}
if(write(accept_fd,buf,ret)<0)
{
perror("error write!\n");
}else
{
printf("service_data_out:%s\n",buf);
}
}
}
close(accept_fd);
close(tcp_fd);
return 0;
}
4.代码调试
使用nc命令测试服务器端socket服务端代码逻辑功能:
(1)代码编写和运行服务
root@wang-virtual-machine:/home/wang/03-linux-net-work/0404-socket# vim service.c
root@wang-virtual-machine:/home/wang/03-linux-net-work/0404-socket# gcc -o service service.c
root@wang-virtual-machine:/home/wang/03-linux-net-work/0404-socket# ./service
(2)在另一个端口测试nc命令
root@wang-virtual-machine:/home/wang# nc 127.0.0.1 9527
(3)调试记录
5.遇到的问题
(1)将sin_addr写成sa_addr;
(2)bind函数参数写错,把sockaddr写成了socketaddr:
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
(3)理解htonl与inet_pton函数的使用场景