通信类型: 控制套接字如何传输和处理数据,数据以包的形式传输
名空间: 指定套接字地址格式
协议: 确定数据如何传输
socket()函数: 创建套接字
int socket(int domain,int type,int protocol);
close()函数: 释放套接字
int close(int fd);
connect()函数: 创建两个套接字之间的连接
int connect(int sockfd,const struct sockaddr* addr,socklen_t addrlen);
send()函数: 发送数据
ssize_t send(int sockfd,const void* buf,size_t len,int flags);
bind()函数: 绑定套接字与其服务器地址
int bind(int sockfd,const struct sockaddr* addr,socklen_t addrlen);
listen()函数: 侦听客户连接
int listen(int sockfd,int backlog);
accept()函数: 接受连接,为该连接创建一个新的套接字
int accept(int sockfd,struct sockaddr* addr,socklen_t addrlen);
#include
#include
#include
#include
#include
#include
#include
//持续读取消息,直到套接字关闭或接收到客户发送的“quit”消息
// 前者返回true,后者返回 false,服务器随后将停止服务
bool Serve(int client_socket)
{
while (true) {
int length;
char* msg;
//从套接字中读取文本消息的长度,返回值为0表示客户连接已关闭
if(read(client_socket,&length,sizeof(length)) == 0)
return true;
msg = new char[length];
read(client_socket,msg,length);
std::cout<std::endl;
if(!strcmp(msg,"quit")) {delete[] msg,msg = NULL;return false;}
else delete[] msg, msg = NULL;
}
}
int main(int argc,char* const argv[])
{
const char* const socket_name = argv[1];
ing socket_fd;
struct sockaddr_un name;
bool serving = true;
//创建套接字
socket_fd = socket(PF_LOCAL,SOCK_STREAM,0);
//设定服务器性质
name.sun_family = AF_LOCAL;
strcpy(name.sun_path,socket_name);
// 绑定套接字
bind(socket_fd,(struct sockaddr*)&name,SUN_LEN(&name));
//侦听客户连接
listen(socket_fd,5);
//重复接受连接,直到某个客户发出“quit”消息
while(serving)
{
struct sockaddr_un client_name;
socklen_t client_name_len;
int client_socket_fd;
//接受客户连接请求
client_socket_fd = accept(socket_fd,(struct sockaddr*)&client_name,&client_name_len);
serving = Serve(client_socket_fd); //服务连接请求
close(socket_fd);//关闭客户连接
}
close(socket_fd);
unlink(socket_name); //删除套接字文件
return 0;
}
#include
#include
#include
#include
#include
#include
#include
void SendMsg(int socket_fd,const char* msg)
{
int length = strlen(msg) + 1;
write(socket_fd,&length,sizeof(length));
write(socket_fd,msg,length);
}
int main(int argc, char const *argv[]) {
const char* const socket_name = argv[1];
const char* const msg = argv[2];
int socket_fd;
struct sockaddr_un name;
//创建套接字
socket_fd = socket(PF_LOCAL,SOCK_STREAM,0);
//套接字地址中存储服务器名称
name.sun_family = AF_LOCAL;
strcpy(name.sun_path,socket_name);
//连接
connect(socket_fd,(struct sockaddr*)&name,SUN_LEN(&name));
//发送消息
SendMsg(socket_fd,msg);
close(socket_fd);
return 0;
}
程序测试运行
./server/tmp/socket;
./server 为服务器端程序名, /tmp/socket 为本服务器启动后的套接字文件名./client/tmp/socket"Hello World!"
; ./client 为客户端程序名./client/tmp/socket"quit"
#include
#include
#include
#include
#include
#include
#include
#include
// 请求Web服务器的主页
void GetHomepage(int socket_fd)
{
char buffer[8192];
sprintf(buffer,"GET /\n");
write(socket_fd,buffer,strlen(buffer));
while(true){
ssize_t count = read(socket_fd,buffer,8192);
if(count == 0) return;
fwrite(buffer,sizeof(char),count,stdout);
}
}
int main(int argc, char const *argv[])
{
int socket_fd;
struct sockaddr_in name;
struct hostent* hostinfo;
socket_fd = socket(PF_INET,SOCK_STREAM,0);
name.sin_family = AF_INET;
hostinfo = gethostbname(argv[1]);
if(hostinfo == NULL) return 1;
else name.sin_addr = *((struct in_addr*)hostinfo ->h_addr);
name.sin_port = htons(80);
if(connect(socket_fd,(struct in_addr*)&name,sizeof(struct sockaddr_in)) == -1) {
perror("Failure to connect the server.");
return 2;
}
GetHomepage(socket_fd);
return 0;
}
“
“`