#include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include<arpa/inet.h> #include<sys/types.h> #include<sys/socket.h> #include<pthread.h> #define SIZE 1024 int client_socket, server_socket; int client_addr_size, flag = 0; char buff_snd[SIZE + 5], buff_rcv[SIZE + 5]; void* snd(void *data){ while(!flag){ gets(buff_rcv); if(strcmp(buff_rcv, "exit") == 0)flag = 1; else{ sprintf( buff_snd, "%d : %s", strlen( buff_rcv), buff_rcv); send(client_socket, buff_snd, strlen(buff_snd)+1, 0); } } } void *rcv(void *data){ int temp; while(!flag){ if(recv(client_socket, buff_rcv, SIZE, 0) <= 0)continue; else{ if(strcmp(buff_rcv, "exit") == 0)flag = 1; else{ printf( "receive: %s\n", buff_rcv); sprintf( buff_snd, "%d : %s", strlen( buff_rcv), buff_rcv); } } } } int main(void){ int option = 1; pthread_t pthread[2]; struct sockaddr_in server_addr, client_addr; server_socket = socket(PF_INET, SOCK_STREAM, 0); setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)); if (-1 == server_socket) { printf( "server socket 생성 실패"); exit( 1) ; } memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(4000); server_addr.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr))){ printf( "bind() 실행 에러\n"); exit( 1); } if(listen(server_socket, 5) == -1){ printf( "대기상태 모드 설정 실패\n"); exit( 1); } client_addr_size = sizeof(client_addr); client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_size); if(client_socket == -1){ printf( "클라이언트 연결 수락 실패\n"); exit(1); } pthread_create(&pthread[0], NULL, snd, (void *)NULL); pthread_create(&pthread[1], NULL, rcv, (void *)NULL); int status; pthread_join(pthread[0], (void**)&status); pthread_join(pthread[1], (void**)&status); printf("채팅이 종료되었습니다.\n"); close(server_socket); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #define SIZE 1024 int flag = 0; char buff_snd[SIZE + 5], buff_rcv[SIZE + 5]; int client_socket; void* snd(void *data){ while(!flag){ gets(buff_rcv); if(strcmp(buff_rcv, "exit") == 0)flag = 1; else{ sprintf( buff_snd, "%d : %s", strlen( buff_rcv), buff_rcv); send(client_socket, buff_snd, strlen(buff_snd)+1, 0); } } } void *rcv(void *data){ int temp; while(!flag){ if(recv(client_socket, buff_rcv, SIZE, 0) <= 0)continue; else{ if(strcmp(buff_rcv, "exit") == 0)flag = 1; else{ printf( "receive: %s\n", buff_rcv); sprintf( buff_snd, "%d : %s", strlen( buff_rcv), buff_rcv); } } } } int main( int argc, char **argv) { pthread_t pthread[2]; struct sockaddr_in server_addr; client_socket = socket( PF_INET, SOCK_STREAM, 0); if( -1 == client_socket) { printf( "socket 생성 실패\n"); exit( 1); } memset( &server_addr, 0, sizeof( server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons( 4000); server_addr.sin_addr.s_addr= inet_addr( "127.0.0.1"); if( -1 == connect( client_socket, (struct sockaddr*)&server_addr, sizeof( server_addr) ) ) { printf( "접속 실패\n"); exit( 1); } pthread_create(&pthread[0], NULL, snd, (void *)NULL); pthread_create(&pthread[1], NULL, rcv, (void *)NULL); int status; pthread_join(pthread[0], (void**)&status); pthread_join(pthread[1], (void**)&status); printf("채팅이 종료되었습니다.\n"); close(client_socket); return 0; }
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include<arpa/inet.h> #include<sys/types.h> #include<sys/socket.h> #include<pthread.h> #define SIZE 1024 #define N 40 int client_socket[N+5], check[N+5], server_socket; int client_addr_size, flag = 0; char buff_snd[SIZE + 5], buff_rcv[SIZE + 5]; void *rcv(void *data){ int i; int temp = (int *)data; while(1){ if(recv(client_socket[temp], buff_rcv, SIZE, 0) <= 0)continue; else{ if(strcmp(buff_rcv, "exit") == 0){ printf("채팅이 종료되었습니다.\n"); break; } printf( "receive: %s\n", buff_rcv); sprintf( buff_snd, "%d : %s", strlen( buff_rcv), buff_rcv); for(i = 1; i<=N; i++){ if(check[i])send(client_socket[i], buff_snd, strlen(buff_snd)+1, 0); } } } close(client_socket[temp]); client_socket[temp] = check[temp] = 0; } int main(void){ int option = 1; pthread_t pthread[N+5]; struct sockaddr_in server_addr, client_addr; int i; server_socket = socket(PF_INET, SOCK_STREAM, 0); setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)); if (-1 == server_socket) { printf( "server socket 생성 실패"); exit( 1) ; } memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(4000); server_addr.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr))){ printf( "bind() 실행 에러\n"); exit( 1); } while(1){ if(listen(server_socket, 5) == -1){ printf( "대기상태 모드 설정 실패\n"); exit( 1); } for(i = 1; i<=N; i++){ if(!check[i]){ client_addr_size = sizeof(client_addr); client_socket[i] = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_size); if(client_socket[i] == -1){ printf( "클라이언트 연결 수락 실패\n"); exit( 1); } check[i] = 1; pthread_create(&pthread[i], NULL, rcv, (void *)i); //int status; //pthread_join(pthread[i], (void **)&status); break; } } } printf("채팅이 종료되었습니다.\n"); close(server_socket); exit(1); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <pthread.h> #define SIZE 1024 int flag1 = 0, flag2 = 0; char buff_snd[SIZE + 5], buff_rcv[SIZE + 5]; int client_socket; void* snd(void *data){ while(!flag1){ while(flag2); gets(buff_rcv); send(client_socket, buff_rcv, strlen(buff_rcv)+1, 0); if(strcmp(buff_rcv, "exit") == 0){ flag1 = 1; printf("채팅이 종료되었습니다.\n"); pthread_exit((void *)NULL); } flag2 = 1; } } void *rcv(void *data){ int temp; while(!flag1){ if(recv(client_socket, buff_rcv, SIZE, 0) <= 0)continue; else{ printf( "receive: %s\n", buff_rcv); sprintf( buff_snd, "%d : %s", strlen( buff_rcv), buff_rcv); flag2 = 0; } } } int main( int argc, char **argv) { pthread_t pthread[2]; struct sockaddr_in server_addr; client_socket = socket( PF_INET, SOCK_STREAM, 0); if( -1 == client_socket) { printf( "socket 생성 실패\n"); exit( 1); } memset( &server_addr, 0, sizeof( server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons( 4000); server_addr.sin_addr.s_addr= inet_addr( "127.0.0.1"); if( -1 == connect( client_socket, (struct sockaddr*)&server_addr, sizeof( server_addr) ) ) { printf( "접속 실패\n"); exit( 1); } pthread_create(&pthread[0], NULL, snd, (void *)NULL); pthread_create(&pthread[1], NULL, rcv, (void *)NULL); int status; pthread_join(pthread[0], (void**)&status); pthread_join(pthread[1], (void**)&status); close(client_socket); exit(1); return 0; }