服務(wù)器與客戶(hù)端簡(jiǎn)單通信——server

<

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),黑龍江企業(yè)網(wǎng)站建設(shè),黑龍江品牌網(wǎng)站建設(shè),網(wǎng)站定制,黑龍江網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,黑龍江網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿(mǎn)足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶(hù)成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。div>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <strings.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>

#define SERV_PORT 9000

#define BUFF_SIZE 1024

struct user
{
int socketfd;
char name[20];
char toName[20];
int result; // 0 代表失敗 1代表成功 2 代表別人給你發(fā)的信息 3代表沒(méi)注冊(cè) 4錯(cuò)誤的指令
char msg[100];
int cmd; // 5 代表注冊(cè)用戶(hù) 10 // 代表發(fā)送信息
};

struct userManage
{
int flag[20]; // 表明哪個(gè)空缺
struct user users[20];
};

pthread_mutex_t mutex;

struct userManage uMge;
void save_user(int socket_fd, struct user * userInfo)
{
pthread_mutex_lock(&mutex);
int i = 0;
for (i = 0; i < 20; i++)
{
if (uMge.flag[i] == 1)
{
continue;
}
uMge.users[i].socketfd = socket_fd;
strcpy(uMge.users[i].name, userInfo->name);
userInfo->result = 1;
uMge.flag[i] = 1;
printf ("%s register success!n", uMge.users[i].name);
break;
}
pthread_mutex_unlock(&mutex);
}

int server_request(int cfd)
{
int readSize, writeSize;
int ret = 0;
int i;
struct user userInfo;
int toname_fd = -1;
int isRegister = -1;
while (readSize = read(cfd, &userInfo, sizeof(userInfo)))
{
if (readSize == -1)
{
perror("read");
return -1;
}
if (userInfo.cmd == 5) // 注冊(cè)用戶(hù)
{
save_user(cfd, &userInfo); // 將用戶(hù)信息進(jìn)行保存
}

else if (userInfo.cmd == 10) // 發(fā)送信息
{
// 檢測(cè)該用戶(hù)是否注冊(cè)
for (i = 0; i < 20; i++)
{
if (uMge.flag[i] == 1)
{
if (uMge.users[i].socketfd == cfd)
{
if (strcmp(uMge.users[i].name, userInfo.name) == 0)
{
isRegister = 1;
}
}
}
}
if(isRegister == 1)
{
// 尋找要發(fā)送的用戶(hù)
for (i = 0; i < 20; i++)
{
if (uMge.flag[i] == 1)
{
// 找到了要發(fā)送的用戶(hù),將其socket保存起來(lái)
if (strcmp(uMge.users[i].name, userInfo.toName) == 0)
{
toname_fd = uMge.users[i].socketfd;
}
}
}
if(toname_fd != -1) // 沒(méi)找到要發(fā)送的用戶(hù)
{
userInfo.result = 2; // 找到用戶(hù)將其返回狀態(tài)標(biāo)為1
writeSize = write(toname_fd, &userInfo, sizeof(userInfo));
if (writeSize == -1)
{
perror("write");
return -1;
}

printf ("%s send msg to %s : %sn", userInfo.name, userInfo.toName, userInfo.msg);
userInfo.result = 1; // 發(fā)送成功
}
}
else
{
userInfo.result = 3; // 沒(méi)有注冊(cè)
}
}
else
{
userInfo.result = 4; // 錯(cuò)誤的指令
}

writeSize = write(cfd, &userInfo, sizeof(userInfo));
if (writeSize == -1)
{
perror("write");
return -1;
}
memset(userInfo.msg, 0, sizeof(userInfo.msg));
}

}

int main()
{
int listen_sockfd;
int ret;
struct sockaddr_in server_addr; // 服務(wù)器地址結(jié)構(gòu)
struct sockaddr_in client_addr; // 客戶(hù)端的地址

pthread_mutex_init(&mutex, NULL);

// 創(chuàng)建監(jiān)聽(tīng)套接字
listen_sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_sockfd == -1)
{
perror("create socket error");
return -1;
}

// 初始化服務(wù)器地址結(jié)構(gòu)
bzero(&server_addr, sizeof(server_addr)); // 將地址結(jié)構(gòu)變量清零
server_addr.sin_family = AF_INET; // 選擇IPV4地址
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // 監(jiān)聽(tīng)本地任意IP地址
server_addr.sin_port = htons(SERV_PORT); // 將本地端口號(hào)轉(zhuǎn)化為網(wǎng)絡(luò)端口號(hào)

// 綁定本地地址和端口號(hào)
ret = bind(listen_sockfd, (struct sockaddr *)&server_addr, sizeof (server_addr));
if (ret == -1)
{
perror ("bind error");
return -1;
}

// 監(jiān)聽(tīng)套接字
ret = listen(listen_sockfd, 20);
if (ret == -1)
{
perror("listen error");
return -1;
}

while(1)
{
int clientfd;
socklen_t client_len = sizeof(client_addr);

clientfd = accept(listen_sockfd, (struct sockaddr *)&client_addr, &client_len);
if (clientfd == -1)
{
perror("accept error");
return -1;
}

pthread_t tid;
// 創(chuàng)建線程處理客戶(hù)端請(qǐng)求
int ret = pthread_create(&tid, NULL, server_request, (void *)clientfd);
if (ret != 0)
{
printf ("create pthread error!n");
return -1;
}
pthread_detach(tid); // 線程分離
}

pthread_mutex_destroy(&mutex);

return 0;
}

分享題目:服務(wù)器與客戶(hù)端簡(jiǎn)單通信——server
當(dāng)前URL:http://www.muchs.cn/article32/cjjpsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷(xiāo)建站公司、域名注冊(cè)、自適應(yīng)網(wǎng)站、響應(yīng)式網(wǎng)站、定制開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷(xiāo)型網(wǎng)站建設(shè)