socket编程步骤
模拟场景
Sockt服务器和客户端的开发步骤
Linux提供的API简析
- 创建套接字(连接协议)
int socket(int domain,int type,int protocol)
- 准备好地址
int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
地址转换API
int inet_aton(const char *straddr, struct in_addr *addrp);
char *inet_ntoa(struct in_addr inaddr);
- 监听
int listen(int sockfd, int backlog);
backlog:支持最大的连接数
- 连接
int accept4(int sockfd, struct sockaddr *addr,socklen_t *addrlen, int flags);
- 数据收发
- 客户端的connect函数
int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
socket服务端代码实现
socket
//1.socket int socket(int domain, int type, int protocol);
s_fd=socket(AF_INET,SOCK_STREAM.0)
//ipv4 tcp协议
if(s_fd == -1){
printf("创建socket失败");
perror("socket:");
exit(-1);
}
bind
//2.bind int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
struct sockaddr_in s_addr;
s_addr.sin_family=AF_INET;//ipv4
s_addr.sin_port=htons(8686);//端口号,选择5000以上。honts返回网络字节序
//int inet_aton(const char *cp, struct in_addr *inp)
inet_aton("127.0.0.1",&s_addr.sin_addr);//转换为网络能识别的格式
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
需要查询结构体struct sockaddr_in
命令
cd /usr/include/
grep "struct sockaddr_in" * -nir
nir:r是递归查询,i是不区分大小写,n是显示行号
listen
//3.listen int listen(int sockfd, int backlog);
listen(s_fd,10);//监听10个连接
accept
//4.accept int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
int client=sizeof(struct sockaddr_in);
c_fd=accept(s_fd,(struct sockaddr *)&c_addr,&client);
if(c_fd == -1){
printf("连接失败\n");
perror("accept:");
exit(-1);
}
printf("客户端的ip:%s\n",inet_ntoa(c_addr.sin_addr)); //把网络格式的ip地址打印成字符串格式
read
//5.read ssize_t read(int fd, void *buf, size_t count);
n_read=read(c_fd,readBuf,128);
if(n_read == -1){
perror("read:");
}else{
printf("得到的消息:%d,%s\n",n_read,readBuf);
}
write
//6.write ssize_t write(int fd, const void *buf, size_t count);
n_write=write(c_fd,returnMsg,strlen(returnMsg));
利用telnet的方式进行通信,目前还没有写客户端的代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
int s_fd;
int c_fd;
int n_read;
int n_write;
char readBuf[128];
char *returnMsg="我收到了你的信息";
struct sockaddr_in c_addr;
struct sockaddr_in s_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
memset(&s_addr,0,sizeof(struct sockaddr_in));//数据清空
//1.socket int socket(int domain, int type, int protocol);
s_fd=socket(AF_INET,SOCK_STREAM,0);
//ipv4 tcp协议
if(s_fd == -1){
printf("创建socket失败");
perror("socket:");
exit(-1);
}
//2.bind int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
s_addr.sin_family=AF_INET;//ipv4
s_addr.sin_port=htons(8603);//端口号,选择5000以上。honts返回网络字节序
//int inet_aton(const char *cp, struct in_addr *inp)
inet_aton("127.0.0.1",&s_addr.sin_addr);//转换为网络能识别的格式
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
//3.listen int listen(int sockfd, int backlog);
listen(s_fd,10);//监听10个连接
//4.accept int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
int client=sizeof(struct sockaddr_in);
c_fd=accept(s_fd,(struct sockaddr *)&c_addr,&client);
if(c_fd == -1){
printf("连接失败\n");
perror("accept:");
exit(-1);
}
printf("客户端的ip:%s\n",inet_ntoa(c_addr.sin_addr)); //把网络格式的ip地址打印成字符串格式
//5.read ssize_t read(int fd, void *buf, size_t count);
n_read=read(c_fd,readBuf,128);
if(n_read == -1){
perror("read:");
}else{
printf("得到的消息:%d,%s\n",n_read,readBuf);
}
//6.write ssize_t write(int fd, const void *buf, size_t count);
n_write=write(c_fd,returnMsg,strlen(returnMsg));
return 0;
}
结果
socket客户端代码实现
初步实现客户端的代码实现通信
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
int c_fd;
int n_read;
int n_write;
int c_connect;
char readBuf[128];
char *returnMsg="这是来自客户端的信息";
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));//数据清空
//1.socket int socket(int domain, int type, int protocol);
c_fd=socket(AF_INET,SOCK_STREAM,0);
//ipv4 tcp协议
if(c_fd == -1){
printf("创建socket失败");
perror("socket:");
exit(-1);
}
c_addr.sin_family=AF_INET;//ipv4
c_addr.sin_port=htons(8603);//端口号,选择5000以上。honts返回网络字节序
//int inet_aton(const char *cp, struct in_addr *inp)
inet_aton("127.0.0.1",&c_addr.sin_addr);//转换为网络能识别的格式
//2.connect int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
c_connect=connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr));
if(c_connect == -1){
printf("连接失败\n");
perror("connect:");
}
//3.write/send ssize_t write(int fd, const void *buf, size_t count);
n_write=write(c_fd,returnMsg,strlen(returnMsg));
//4.read ssize_t read(int fd, void *buf, size_t count);
n_read=read(c_fd,readBuf,128);
if(n_read == -1){
perror("read:");
}else{
printf("来自服务端的消息:%d,%s\n",n_read,readBuf);
}
//5.close int close(int fd);
close(c_fd);
return 0;
}
结果
实现双方聊天(服务端与客户端)
服务端代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
int s_fd;
int c_fd;
int n_read;
int n_write;
char readBuf[128];
char returnMsg[128]={0};
struct sockaddr_in c_addr;
struct sockaddr_in s_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
memset(&s_addr,0,sizeof(struct sockaddr_in));//数据清空
if(argc != 3){
printf("参数出错\n");
exit(-1);
}
//1.socket int socket(int domain, int type, int protocol);
s_fd=socket(AF_INET,SOCK_STREAM,0);
//ipv4 tcp协议
if(s_fd == -1){
printf("创建socket失败");
perror("socket:");
exit(-1);
}
//2.bind int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
s_addr.sin_family=AF_INET;//ipv4
s_addr.sin_port=htons(atoi(argv[2]));//端口号,选择5000以上。honts返回网络字节序,atoi(argv[2])防止端口被占用
//int inet_aton(const char *cp, struct in_addr *inp)
inet_aton(argv[1],&s_addr.sin_addr);//转换为网络能识别的格式
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
//3.listen int listen(int sockfd, int backlog);
listen(s_fd,10);//监听10个连接
//4.accept int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
int client=sizeof(struct sockaddr_in);
while(1){//不断接收客户端
c_fd=accept(s_fd,(struct sockaddr *)&c_addr,&client);
if(c_fd == -1){
printf("连接失败\n");
perror("accept:");
exit(-1);
}
printf("客户端的ip:%s\n",inet_ntoa(c_addr.sin_addr)); //把网络格式的ip地址打印成字符串格式
if(fork() == 0){
if(fork() == 0){
while(1){//不断写入
memset(returnMsg,0,sizeof(returnMsg));
printf("请输入:\n");
gets(returnMsg);
//6.write ssize_t write(int fd, const void *buf, size_t count);
n_write=write(c_fd,returnMsg,strlen(returnMsg));
}
}
while(1){//不断读取
//5.read ssize_t read(int fd, void *buf, size_t count);
memset(readBuf,0,sizeof(readBuf));//不断清空数据防止数据重复出现
n_read=read(c_fd,readBuf,128);
if(n_read == -1){
perror("read:");
}else{
printf("得到的消息:%d,%s\n",n_read,readBuf);
}
}
}
}
return 0;
}
客户端代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
int ret;
int c_fd;
int n_read;
int n_write;
int c_connect;
char readBuf[128];
char returnMsg[128]={0};
char *quit="quit";
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));//数据清空
if(argc != 3){
printf("参数出错\n");
exit(-1);
}
//1.socket int socket(int domain, int type, int protocol);
c_fd=socket(AF_INET,SOCK_STREAM,0);
//ipv4 tcp协议
if(c_fd == -1){
printf("创建socket失败");
perror("socket:");
exit(-1);
}
c_addr.sin_family=AF_INET;//ipv4
c_addr.sin_port=htons(atoi(argv[2]));//端口号,选择5000以上。honts返回网络字节序
//int inet_aton(const char *cp, struct in_addr *inp)
inet_aton(argv[1],&c_addr.sin_addr);//转换为网络能识别的格式
//2.connect int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
c_connect=connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr));
if(c_connect == -1){
printf("连接失败\n");
perror("connect:");
}
while(1){
if(fork() == 0){
while(1){//不断写入
memset(returnMsg,0,sizeof(returnMsg));
printf("请输入:\n");
gets(returnMsg);
//3.write/send ssize_t write(int fd, const void *buf, size_t count);
n_write=write(c_fd,returnMsg,strlen(returnMsg));
if(strcmp(quit,returnMsg) == 0){//如果输入quit则客户端就退出
exit(0);
}
}
}
while(1){//不断读取
memset(readBuf,0,sizeof(readBuf));//不断清空数据防止数据重复出现
//4.read ssize_t read(int fd, void *buf, size_t count);
n_read=read(c_fd,readBuf,128);
if(n_read == -1){
perror("read:");
}else{
printf("来自服务端的消息:%d,%s\n",n_read,readBuf);
}
}
//5.close int close(int fd);
close(c_fd);
}
return 0;
}
结果
多方消息收发
把服务端当成显示信息信息的平台,使连接进来的客户端能够查看自己发送的信息以及信息共享,不同客户端可以进行聊天对话
服务端代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
int mark=0;
int s_fd;
int c_fd;
int n_read;
int n_write;
char readBuf[128];
char returnMsg[128]={0};
struct sockaddr_in c_addr;
struct sockaddr_in s_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
memset(&s_addr,0,sizeof(struct sockaddr_in));//数据清空
if(argc != 3){
printf("参数出错\n");
exit(-1);
}
//1.socket int socket(int domain, int type, int protocol);
s_fd=socket(AF_INET,SOCK_STREAM,0);
//ipv4 tcp协议
if(s_fd == -1){
printf("创建socket失败");
perror("socket:");
exit(-1);
}
//2.bind int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
s_addr.sin_family=AF_INET;//ipv4
s_addr.sin_port=htons(atoi(argv[2]));//端口号,选择5000以上。honts返回网络字节序,atoi(argv[2])防止端口被占用
//int inet_aton(const char *cp, struct in_addr *inp)
inet_aton(argv[1],&s_addr.sin_addr);//转换为网络能识别的格式
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
//3.listen int listen(int sockfd, int backlog);
listen(s_fd,10);//监听10个连接
//4.accept int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
int client=sizeof(struct sockaddr_in);
while(1){//不断接收客户端
c_fd=accept(s_fd,(struct sockaddr *)&c_addr,&client);
if(c_fd == -1){
printf("连接失败\n");
perror("accept:");
exit(-1);
}
printf("客户端的ip:%s\n",inet_ntoa(c_addr.sin_addr)); //把网络格式的ip地址打印成字符串格式
mark++;
if(fork() == 0){
if(fork() == 0){
while(1){
sprintf(returnMsg,"欢迎第%d号客户端",mark);
//6.write ssize_t write(int fd, const void *buf, size_t count);
n_write=write(c_fd,returnMsg,strlen(returnMsg));
sleep(20);
}
}
while(1){
//5.read ssize_t read(int fd, void *buf, size_t count);
memset(readBuf,0,sizeof(readBuf));
n_read=read(c_fd,readBuf,128);
if(n_read == -1){
perror("read:");
}else{
printf("得到%d号的消息:%s\n",mark,readBuf);
}
}
}
}
return 0;
}
客户端代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
int c_fd;
int n_read;
int n_write;
int c_connect;
char readBuf[128];
char returnMsg[128]={0};
char *quit="quit";
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));//数据清空
if(argc != 3){
printf("参数出错\n");
exit(-1);
}
//1.socket int socket(int domain, int type, int protocol);
c_fd=socket(AF_INET,SOCK_STREAM,0);
//ipv4 tcp协议
if(c_fd == -1){
printf("创建socket失败");
perror("socket:");
exit(-1);
}
c_addr.sin_family=AF_INET;//ipv4
c_addr.sin_port=htons(atoi(argv[2]));//端口号,选择5000以上。honts返回网络字节序
//int inet_aton(const char *cp, struct in_addr *inp)
inet_aton(argv[1],&c_addr.sin_addr);//转换为网络能识别的格式
//2.connect int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
c_connect=connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr));
if(c_connect == -1){
printf("连接失败\n");
perror("connect:");
}
while(1){
if(fork() == 0){
while(1){//不断写入
memset(returnMsg,0,sizeof(returnMsg));
printf("请输入:\n");
gets(returnMsg);
//3.write/send ssize_t write(int fd, const void *buf, size_t count);
n_write=write(c_fd,returnMsg,strlen(returnMsg));
if(strcmp(quit,returnMsg) == 0){//如果输入quit则客户端就退出
exit(0);
}
}
}
while(1){//不断读取
memset(readBuf,0,sizeof(readBuf));//不断清空数据防止数据重复出现
//4.read ssize_t read(int fd, void *buf, size_t count);
n_read=read(c_fd,readBuf,128);
if(n_read == -1){
perror("read:");
}else{
printf("来自服务端的消息:%s\n",readBuf);
}
}
//5.close int close(int fd);
close(c_fd);
}
return 0;
}