2010年11月4日 星期四

Coding-Socket: Client &Server In C

Client.c




======================================================


#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <unistd.h> #include <sys/types.h> #include <netinet in.h> #include <sys/socket.h> int main(int argc, char* argv[]){ int sockfd, numbytes; char buf[100]; struct sockaddr_in address; //TCP socket if ( ( sockfd = socket(AF_INET, SOCK_STREAM, 0) ) == -1 ){ perror("socket"); exit(1); } //Initial, connect to port 2323 address.sin_family = AF_INET; address.sin_port = htons(2323); address.sin_addr.s_addr = inet_addr("127.0.0.1"); bzero( &(address.sin_zero), 8 ); //Connect to server if ( connect(sockfd, (struct sockaddr*)&address, sizeof(struct sockaddr)) == -1){ perror("connect"); exit(1); } sprintf(buf,"mkdir -p abc/de/fghi/aaa.m2v"); //Send "Hello"; if ( (numbytes = write(sockfd, buf, strlen(buf)) )== -1 ){ perror("send"); exit(1); } printf("Write %d bytes , ",numbytes); //Receive from server if ( (numbytes = read(sockfd, buf, strlen(buf)) ) == -1 ){ perror("recv"); exit(1); } printf("Read %d bytes\n",numbytes); buf[numbytes] = '\0'; printf("result: %s \n", buf); close(sockfd); return 0; } ======================================================


Server.c


======================================================

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <unistd.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

#include <sys/wait.h>



int main(){

int sockfd, new_fd, numbytes;

struct sockaddr_in my_addr;

struct sockaddr_in their_addr;

int sin_size;

char buf[100];



//TCP socket

if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){

perror("socket");

exit(1);

}



//Initail, bind to port 2323 

my_addr.sin_family = AF_INET;

my_addr.sin_port = htons(2323);

my_addr.sin_addr.s_addr = htonl(INADDR_ANY);

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



//binding

if ( bind(sockfd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)) == -1 ){

perror("bind");

exit(1);

}



//Start listening

if ( listen(sockfd, 10) == -1 ){

perror("listen");

exit(1);

}



//Wait for connect!

while(1){

sin_size = sizeof(struct sockaddr_in);

perror("server is run");

if ( (new_fd = accept(sockfd, (struct sockaddr*)&their_addr, &sin_size)) == -1 ){

perror("accept");

exit(1);

}



if ( !fork() ){

//Receive

if ( (numbytes = read(new_fd, buf, sizeof(buf))) == -1 ){

perror("recv");

exit(1);

}

printf("Receive %d bytes , ", numbytes);

printf("%c\n", buf[27]);

buf[numbytes]='\0';

//Send back

if ( (numbytes = write(new_fd, buf, /*strlen(buf)*/numbytes)) == -1){

perror("send");

exit(0);

}

printf("Send %d bytes , ", numbytes);

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

close(new_fd);

}

}

close(sockfd);

return 0;

}



exit(1);

}



if ( !fork() ){

//Receive

if ( (numbytes = read(new_fd, buf, sizeof(buf))) == -1 ){

perror("recv");

exit(1);

}

printf("Receive %d bytes , ", numbytes);

printf("%c\n", buf[27]);

buf[numbytes]='\0';

//Send back

if ( (numbytes = write(new_fd, buf, /*strlen(buf)*/numbytes)) == -1){

perror("send");

exit(0);

}

printf("Send %d bytes , ", numbytes);

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

close(new_fd);

}

}

close(sockfd);

return 0;

}



======================================================





用gcc把這兩支編譯出來之後,先執行server再執行client就OK了


$gcc server.c -o server
$gcc client.c -o client
Terminal1
$./server
Terminal2
$./client

沒有留言: