日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

ubuntu系统安装socket服务器,Ubuntu上进行socket编程,并且实现通信功能

發(fā)布時(shí)間:2023/12/2 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ubuntu系统安装socket服务器,Ubuntu上进行socket编程,并且实现通信功能 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define PERM S_IRUSR|S_IWUSR

#define MYPORT 5500 //宏定義,定義通信端口

#define BACKLOG 10

//宏定義,定義服務(wù)程序可以連接的最大客戶(hù)數(shù)量

#define WELCOME ?"|---------Welcome to the

chat room!----------|"

//宏定義,當(dāng)客戶(hù)端連接到服務(wù)器時(shí),向客戶(hù)發(fā)送此歡迎字符

//轉(zhuǎn)換函數(shù),將int類(lèi)型轉(zhuǎn)換成char*類(lèi)型

void itoa(int i,char*string)

{

int power,j;

j=i;

for(power=1;j>=10;j/=10)

power*=10;

for(;power>0;power/=10)

{

*string++='0'+i/power;

i%=power;

}

*string='\0';

}

//得到當(dāng)前系統(tǒng)的時(shí)間

void get_cur_time(char *time_str)

{

time_t timep;

struct tm *p_curtime;

char *time_tmp;

time_tmp = (char*)malloc(2);

memset(time_tmp,0,2);

memset(time_str,0,20);

time(&timep);

p_curtime = localtime(&timep);

strcat(time_str,"(");

itoa(p_curtime->tm_hour,time_tmp);

strcat(time_str,time_tmp);

strcat(time_str,":");

itoa(p_curtime->tm_min,time_tmp);

strcat(time_str,time_tmp);

strcat(time_str,":");

itoa(p_curtime->tm_sec,time_tmp);

strcat(time_str,time_tmp);

strcat(time_str,")");

free(time_tmp);

}

//創(chuàng)建共享存儲(chǔ)區(qū)

key_t shm_create()

{

key_t shmid;

if((shmid = shmget(IPC_PRIVATE,1024,PERM)) == -1)

{

fprintf(stderr,"Create Share Memory

Error:%s\n\a",strerror(errno));

exit(1);

}

return shmid;

}

//端口綁定函數(shù),創(chuàng)建套件字,并綁定到指定端口

int bindPort(unsigned short int port)

{

int sockfd;

struct sockaddr_in my_addr;

sockfd = socket(AF_INET,SOCK_STREAM,0);//創(chuàng)建基于六套接字

my_addr.sin_family = AF_INET; //IPv4協(xié)議族

my_addr.sin_port = htons(port);//端口轉(zhuǎn)換

my_addr.sin_addr.s_addr = INADDR_ANY;

bzero(&(my_addr.sin_zero),0);

if(bind(sockfd,(struct

sockaddr*)&my_addr,sizeof(struct sockaddr)) ==

-1)

{

perror("bind");

exit(1);

}

printf("bind success!\n");

return sockfd;

}

void write_in_file(char* buf_in)

{

FILE *fp;

if((fp = fopen("/home/wbb/workspace/1.txt","a+")) ==

NULL)

{

printf("文件不能打開(kāi)!\n");

exit(1);

}

fprintf(fp,"%s\n",buf_in);

fclose(fp);

}

int main(int argc,char *argv[])

{

socklen_t sockfd,clientfd,recvbytes;//定義監(jiān)聽(tīng)套接字、客戶(hù)套接字

socklen_t sin_size;

pid_t pid,ppid; //定義父子線程標(biāo)記變量 pid_t == Process ID_Type

宏定義insigned int 類(lèi)型

char *buf,*r_addr,*w_addr,*temp,*time_str; //定義臨時(shí)存儲(chǔ)區(qū)

struct sockaddr_in their_addr; //定義地址結(jié)構(gòu)

key_t shmid;

shmid = shm_create(); //創(chuàng)建共享存儲(chǔ)區(qū)

temp = (char*)malloc(255);

time_str = (char*)malloc(20);

sockfd = bindPort(MYPORT); //綁定端口

while(1)

{

if(listen(sockfd,BACKLOG) == -1) //指定端口上監(jiān)聽(tīng)

{

perror("listen");

exit(1);

}

printf("listening......\n");

if((clientfd = accept(sockfd,(struct

sockaddr*)&their_addr,&sin_size))

== -1)

{

perror("accept");

exit(1);

}

printf("accept from:

%s\n",inet_ntoa(their_addr.sin_addr));

send(clientfd,WELCOME,strlen(WELCOME),0);//發(fā)送問(wèn)候消息

buf = (char*)malloc(255);

ppid = fork();//創(chuàng)建子進(jìn)程

if(ppid == 0)

{

pid = fork(); //創(chuàng)建子進(jìn)程

while(1)

{

if(pid >0)

{

//父進(jìn)程用于接收信息

memset(buf,0,255);

if((recvbytes =

recv(clientfd,buf,255,0))<=0)

{

perror("recv1");

close(clientfd);

raise(SIGKILL);

exit(1);

}

//write buf's data to share memory

w_addr = shmat(shmid,0,0);

memset(w_addr,'\0',1024);

strncpy(w_addr,buf,1024);

get_cur_time(time_str);

strcat(buf,time_str);

printf("接收的:%s\n",buf);

write_in_file(buf);

}

else if(pid == 0)

{

//子進(jìn)程用于發(fā)送信息

//scanf("%s",buf);

sleep(1);

r_addr = shmat(shmid,0,0);

if(strcmp(temp,r_addr)!=0)

{

strcpy(temp,r_addr);

get_cur_time(time_str);

strcat(r_addr,time_str);

if(send(clientfd,r_addr,strlen(r_addr),0) == -1)

{

perror("send");

}

memset(r_addr,'\0',1024);

strcpy(r_addr,temp);

}

}

else

perror("fork");

}

}

}

printf("------------------------\n");

free(buf);

close(sockfd);

close(clientfd);

return 0;

}

客戶(hù)端代碼:

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的ubuntu系统安装socket服务器,Ubuntu上进行socket编程,并且实现通信功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。