unix socket通訊

來源:互聯網
上載者:User

賈大神寫的這些socket代碼還是挺不錯的,夠規範,但是貌似還需要我改一改.先收藏一下吧.

用戶端

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
/**
 *title: 似乎是之前的上機練習
 *Author:Eric Jia
 *desc: 網路編程似乎是要發送一段訊息給另一方接收,互相聊天
 *add: 這是用戶端,運行是需要先運行服務端,然後再運行用戶端。用戶端運行時候請把伺服器ip作為參數加入。
 * 比如本機通訊的話,再運行了服務端之後,運行用戶端: ./client 127.0.0.1
 * 然後用戶端和服務端就可以聊high了。。 
 */
#define SEND_MAX 256
#define PORT 7227

void recv_try(int sockfd){
int n;
char buf[SEND_MAX];
while((n=recv(sockfd, buf, SEND_MAX, 0))>0){
printf("**Others Said:**\r\n");

printf("%s\r\n", buf);
printf("******\n\n");

}
if(n<0)
printf("**Recive Error**\n");
else
printf("**Seems Server Has Disconnected Connection**\n");
}

void err_exit(char* msg){
printf("%s",msg);
exit(1);
return;
}

void err(char* msg){
printf("%s",msg);
return ;
}

int main(int argc, char* argv[]){
int sock;
struct sockaddr_in sin;

if(argc!=2){
err_exit("usage: [ProgramName] [HostIP]\r\n");

}else if((sock=socket(AF_INET, SOCK_STREAM, 0))<0){
err_exit("**Seems a Error Caused In Getting a Socket **\r\n");
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr= inet_addr(argv[1]);
sin.sin_port = PORT;

if(connect(sock, (struct sockaddr*)&sin, sizeof(sin))<0)
{
err_exit("**Seems the Server is Not Ready **\r\n");
}else{
printf("**Successful Connected! ;D**\n\n");
}
pid_t pid;
if((pid=fork())<0)
err_exit("Fork Error\r\n");
else if(pid==0){
recv_try(sock);
}else{
while(1){
char buf[SEND_MAX];
memset(buf,0,SEND_MAX);
gets(buf);
if(!strcmp(buf,"::cmd_exit")){
close(sock);

break;
}
write(sock,buf,SEND_MAX);
printf("\n");
}
}
return 0;

}

伺服器端:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
/**
 *title: 似乎是之前的上機練習
 *Author:Eric Jia
 *desc: 網路編程似乎是要發送一段訊息給另一方接收,互相聊天
 *add: 這是用戶端,運行是需要先運行服務端,然後再運行用戶端。用戶端運行時候請把伺服器ip作為參數加入。
 * 比如本機通訊的話,再運行了服務端之後,運行用戶端: ./client 127.0.0.1
 * 然後用戶端和服務端就可以聊high了。。 
 */
#define SEND_MAX 256
#define PORT 7227
#define MAX_CONNECTION 10

void err_exit(char* msg){
printf("%s",msg);
exit(1);
return;
}

void err(char* msg){
printf("%s",msg);
return ;
}

void recv_try(int sockfd){
int n;
char buf[SEND_MAX];
while((n=recv(sockfd, buf, SEND_MAX, 0))>0){
printf("**Others Said:**\r\n");

printf("%s\r\n", buf);
printf("******\n\n");

}
if(n<0)
printf("**Recive Error**\n");
else
printf("**Seems Client Has Disconnected Connection**\n");
}

int main(int argc, char* argv[]){
int sock;
struct sockaddr_in sin;

  if((sock=socket(AF_INET, SOCK_STREAM, 0))<0){
err_exit("**Seems a Error Caused In Getting a Socket **\n");
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr= inet_addr("127.0.0.1");
sin.sin_port = PORT;
if(bind(sock,(struct sockaddr*)&sin,sizeof(sin)))
err_exit("Err Bind\n");
else if(listen(sock,MAX_CONNECTION))
err_exit("ERR Listen\n");
struct sockaddr_in client;
int sin_size=sizeof(client);
int new_sock = accept(sock, (struct sockaddr* )&client, (socklen_t* )&sin_size);

pid_t pid;
if((pid=fork())<0)
err_exit("Fork Error\n");
else if(pid==0){
recv_try(new_sock);
}else{
while(1){
char buf[SEND_MAX];
memset(buf,0,SEND_MAX);
gets(buf);
if(!strcmp(buf,"::cmd_exit")){
close(new_sock);

break;
}
write(new_sock,buf,SEND_MAX);
printf("\n");
}
}

return 0;

}

明天考完試之後有時間給它改一改

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.