Created
October 9, 2013 00:46
-
-
Save crouchggj/6894348 to your computer and use it in GitHub Desktop.
Linux Socket Program(TCP client & server)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ===================================================================================== | |
* | |
* Filename: client.c | |
* | |
* Description: | |
* | |
* Version: 1.0 | |
* Created: 2013年08月19日 17时19分33秒 | |
* Revision: none | |
* Compiler: gcc | |
* | |
* Author: YOUR NAME (), | |
* Organization: | |
* | |
* ===================================================================================== | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <string.h> | |
#include <error.h> | |
#include <netdb.h> | |
#include <sys/types.h> | |
#include <netinet/in.h> | |
#include <sys/socket.h> | |
#define SEVPORT 3333 | |
#define MAXDATASIZE (1024*5) | |
#define TIME_DIFF(t1,t2) (((t1).tv_sec-(t2).tv_sec)*1000+((t1).tv_usec-(t2).tv_usec)/1000) | |
main(int argc, char *argv[]) | |
{ | |
int sockfd,sendbytes,recvbytes; | |
char buf[MAXDATASIZE]; | |
struct hostent *host; | |
struct sockaddr_in serv_addr; | |
struct timeval timestamp; | |
struct timeval timestamp_end; | |
if(argc < 2){ | |
fprintf(stderr,"Please enter the server's hostname!\n"); | |
exit(1); | |
} | |
if((host=gethostbyname(argv[1])) == NULL){ | |
perror("gethostbyname:"); | |
exit(1); | |
} | |
#ifdef CLIENT_DEBUG | |
printf("hostent h_name: %s , h_aliases: %s,\ | |
h_addrtype: %d, h_length: %d, h_addr_list: %s\n",\ | |
host->h_name,*(host->h_aliases),host->h_addrtype,host->h_length,*(host->h_addr_list)); | |
#endif | |
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){ | |
perror("socket:"); | |
exit(1); | |
} | |
serv_addr.sin_family = AF_INET; | |
serv_addr.sin_port = htons(SEVPORT); | |
serv_addr.sin_addr = *((struct in_addr *)host->h_addr); | |
bzero(&(serv_addr.sin_zero),8); | |
if(connect(sockfd,(struct sockaddr *)&serv_addr, \ | |
sizeof(struct sockaddr)) == -1){ | |
perror("connect:"); | |
exit(1); | |
} | |
memset(buf,0x15, sizeof(buf)); | |
gettimeofday(×tamp,NULL); | |
if((sendbytes = send(sockfd,buf,sizeof(buf),0)) == -1){ | |
perror("send:"); | |
exit(1); | |
} | |
gettimeofday(×tamp_end, NULL); | |
printf("sendbytes: %d,time_stamp: %ld ms\n",sendbytes,TIME_DIFF(timestamp_end,timestamp)); | |
if((recvbytes = recv(sockfd, buf,MAXDATASIZE,0)) == -1) | |
{ | |
perror("recv"); | |
close(sockfd); | |
exit(1); | |
} | |
printf("Client receive bytes: %d\n",recvbytes); | |
close(sockfd); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ===================================================================================== | |
* | |
* Filename: server.c | |
* | |
* Description: | |
* | |
* Version: 1.0 | |
* Created: 2013年08月19日 16时26分25秒 | |
* Revision: none | |
* Compiler: gcc | |
* | |
* Author: YOUR NAME (), | |
* Organization: | |
* | |
* ===================================================================================== | |
*/ | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <netinet/in.h> | |
#define SERVPORT 3333 | |
#define BACKLOG 10 | |
#define MAX_CONNECTED_NO 10 | |
#define MAXDATASIZE (5*1024) | |
int main() | |
{ | |
struct sockaddr_in server_sockaddr,client_sockaddr; | |
int sin_size,recvbytes,sendbytes; | |
int sockfd,client_fd; | |
char buf[MAXDATASIZE]; | |
if((sockfd = socket(AF_INET, SOCK_STREAM,0)) == -1) | |
{ | |
perror("socket:"); | |
exit(1); | |
} | |
int on; | |
on = 1; | |
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); | |
server_sockaddr.sin_family=AF_INET; | |
server_sockaddr.sin_port = htons(SERVPORT); | |
server_sockaddr.sin_addr.s_addr=INADDR_ANY; | |
memset(&(server_sockaddr.sin_zero),0,8); | |
if((bind(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr))) == -1) | |
{ | |
perror("bind:"); | |
exit(1); | |
} | |
if(listen(sockfd,BACKLOG) == -1) | |
{ | |
perror("listen:"); | |
exit(1); | |
} | |
printf("Start listen.....\n"); | |
sin_size = sizeof(struct sockaddr); | |
if((client_fd = accept(sockfd,(struct sockaddr *)&client_sockaddr,&sin_size)) == -1) | |
{ | |
perror("accept:"); | |
exit(1); | |
} | |
struct sockaddr_in peeraddr; | |
int len = sizeof(peeraddr); | |
char server_ip[20]; | |
printf("accept socket..... Client address: %s Port: %d\n",\ | |
inet_ntoa(client_sockaddr.sin_addr),ntohs(client_sockaddr.sin_port)); | |
getsockname(client_fd,(struct sockaddr *)&peeraddr,&len); | |
inet_ntop(AF_INET, &peeraddr.sin_addr,server_ip,sizeof(server_ip)); | |
printf("accept socket..... Server address: %s\n",server_ip); | |
if((recvbytes=recv(client_fd,buf,MAXDATASIZE,0)) == -1) | |
{ | |
perror("recv:"); | |
exit(1); | |
} | |
printf("Recv bytes: %d, buf: 0x%x\n",recvbytes, buf[2]); | |
if((sendbytes = send(client_fd,buf,sizeof(buf),0)) == -1) | |
{ | |
perror("send:"); | |
exit(1); | |
} | |
printf("Send butes: %d \n",sendbytes); | |
close(sockfd); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment