y0u_bat
C언어 - 소켓 프로그래밍(Server & Client 문자열 주고 받기) 예제 본문
Server.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <fcntl.h> #include <unistd.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/types.h> #include <arpa/inet.h> #define BUF_LEN 128 int main(int argc,char *argv[]) { char buffer[BUF_LEN]; struct sockaddr_in server_addr,client_addr; char temp[20]; int server_fd,client_fd; int len,msg_size; char test[20]; if(argc !=2) { printf("Usege ./filename [PORT] \n"); exit(0); } if((server_fd = socket(AF_INET,SOCK_STREAM,0)) == -1) { printf("Server: can not Open Socket\n"); exit(0); } memset(&server_addr,0x00,sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_ANY); server_addr.sin_port = htons(atoi(argv[1])); if(bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr))< 0) { printf("Server: cat not bind local addrss\n"); exit(0); } if(listen(server_fd,5) < 0) { printf("Server: cat not listen connnect.\n"); exit(0); } memset(buffer,0x00,sizeof(buffer)); len = sizeof(client_addr); printf("=====[PORT] : %d =====\n",atoi(argv[1])); printf("Server : wating connection request.\n"); while(1) { client_fd = accept(server_fd,(struct sockaddr *)&client_addr,(socklen_t *)&len); if(client_fd < 0) { printf("Server: accept failed\n"); exit(0); } inet_ntop(AF_INET,&client_addr.sin_addr.s_addr,temp,sizeof(temp)); printf("Server: %s client connect.\n",temp); send(client_fd,"test test test",0xff,0); msg_size = recv(client_fd,(char *)buffer,BUF_LEN,0); send(client_fd,(char *)buffer,msg_size,0); close(client_fd); printf("Server: %s client closed.\n",temp); } close(server_fd); return 0; } | cs |
Client.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/socket.h> #include <sys/types.h> #define BUF_SIZE 1024 #define PORT 1234 #define IPADDR "127.0.0.1" int main(int argc,char *argv[]) { int client_fd,len; struct sockaddr_in client_addr; char recv_data[BUF_SIZE]; client_fd = socket(PF_INET,SOCK_STREAM,0); client_addr.sin_addr.s_addr = inet_addr(IPADDR); client_addr.sin_family = AF_INET; client_addr.sin_port = htons(PORT); if(connect(client_fd,(struct sockaddr *)&client_addr,sizeof(client_addr))== -1) { printf("Can't connect\n"); close(client_fd); return -1; } recv(client_fd,recv_data,sizeof(recv_data),0); printf("recv data : %s\n",recv_data); close(client_fd); return 0; } | cs |
Server
Client
'프로그래밍 > C언어' 카테고리의 다른 글
c언어 - pthread 쓰레드 (0) | 2015.11.19 |
---|---|
c언어 - 소켓프로그래밍(1:1 채팅) (3) | 2015.11.19 |
C언어 - 링크드리스트 linklist(추가,수정,삭제,검색,출력) 예제 (8) | 2015.11.16 |
C언어 - strcpy 함수구현 (0) | 2015.10.07 |
C언어 - strchr 함수구현 (0) | 2015.10.07 |
Comments