概述
套接字socket---网络套接字
网络套接字
- 采用回路网络连接到一个网络套接字
- 回路网路只包含一台计算机,通常称为localhost,IP地址为127.0.0.1,可以在/etc/hosts文件中找到
例子:
客户端发一个A给服务器,服务器收到A,加1改成B,返回给客户端,客户端显示收到的字符
server1.c
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main()
{
int server_sockfd, client_sockfd;
int server_len, client_len;
//sockaddr_in:网络套接字的地址
struct sockaddr_in server_address;
struct sockaddr_in client_address;
//第一个参数域AF_INET
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
//命名网络套接字
server_address.sin_family = AF_INET;
//inet_addr:把一个字符串形式的IP地址变成整数
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
server_address.sin_port = 9734;
server_len = sizeof(server_address);
//套接字命名
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
//创建长度为5的监听队列
listen(server_sockfd, 5);
while(1)
{
char ch;
printf("server waitingn");
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
read(client_sockfd, &ch, 1);
ch++;
write(client_sockfd, &ch, 1);
close(client_sockfd);
}
}
client1.c
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main()
{
int sockfd;
int len;
struct sockaddr_in address;//地址格式
int result;
char ch = 'A';
sockfd = socket(AF_INET, SOCK_STREAM, 0);//生成套接字
address.sin_family = AF_INET;
//inet_addr的返回值已经是网络字节序
address.sin_addr.s_addr = inet_addr("127.0.0.1");//格式转换
address.sin_port = 9734;
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if (result == -1)
{
perror("oops:client2");
exit(1);
}
write(sockfd, &ch, 1);
read(sockfd, &ch, 1);
printf("char from server = %cn", ch);
close(sockfd);
exit(0);
}
字节序问题
不同计算机的字节序不一样,例如intel机器一般采用小端方式,而motorola一般采用大端方式,因此传输信息时会造成混乱。
网络字节序:客户和服务器在传输之前,将内部整数表示方式转换为网络字节序可解决字节序问题。
#include <netinet/in.h>
unsigned long int htonl(unsigned long int hostlong); //host to net本地字节序转换为网络字节序
unsigned short int htons(unsigned short int hostshort); //host to net本地字节序转换为网络字节序
unsigned long int ntohl(unsigned long int netlong); //net to host网络字节序转换为本地字节序
unsigned short int ntohs(unsigned short int netshort); //net to host网络字节序转换为本地字节序
server2.c
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main()
{
int server_sockfd, client_sockfd;
int server_len, client_len;
//sockaddr_in:网络套接字的地址
struct sockaddr_in server_address;
struct sockaddr_in client_address;
//第一个参数域AF_INET
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
//命名网络套接字
server_address.sin_family = AF_INET;
//INADDR_ANY:任何机器都允许连接服务器上
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(9734);
server_len = sizeof(server_address);
//套接字命名
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
//创建长度为5的监听队列
listen(server_sockfd, 5);
while(1)
{
char ch;
printf("server waitingn");
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
read(client_sockfd, &ch, 1);
ch++;
write(client_sockfd, &ch, 1);
close(client_sockfd);
}
}
client2.c
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main()
{
int sockfd;
int len;
struct sockaddr_in address;//地址格式
int result;
char ch = 'A';
sockfd = socket(AF_INET, SOCK_STREAM, 0);//生成套接字
address.sin_family = AF_INET;
//inet_addr的返回值已经是网络字节序
address.sin_addr.s_addr = inet_addr("127.0.0.1");//格式转换
address.sin_port = htons(9734);
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if (result == -1)
{
perror("oops:client2");
exit(1);
}
write(sockfd, &ch, 1);
read(sockfd, &ch, 1);
printf("char from server = %cn", ch);
close(sockfd);
exit(0);
}
最后
以上就是跳跃枕头为你收集整理的套接字socket---网络套接字的全部内容,希望文章能够帮你解决套接字socket---网络套接字所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复