unix socket通訊

鴨脖發表於2012-06-29

賈大神寫的這些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;
}

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

相關文章