c++ 도와주세요 소켓, 쓰레드
얀별
질문 제목 :소켓을 쓰레드로 만들기
질문 요약 :소켓프로그래밍에서 쓰레드를 적용하여 여러 클라이언트 접속을 허용하고 싶은데요.
서버가 안띄워지고 그냥 종료되어 버리네요 ㅜㅜ
질문 내용 : while문안에서 계속 돌아야할 것들이 그냥 돌지도 않고 끝나버려요 흑흑
컴파일할때 에러는 안나는데요 ....
server.h
#includestdio.h
#includestdlib.h
#includestring
#includememory.h
#includeunistd.h
#includearpa/inet.h
#includesys/types.h
#includesys/socket.h
#includeiostream
#define BUFSIZE 100
#define TcpPort 9999// TCP Port Number
using namespace std;
struct C_Info
{
int TSocket;
in_addr ClientIP;
};
class TSocket
{
public:
static void error(string message);
static void *Try(void *arg);
static void *Connect(void *ptr);
}; #include server.h
void TSocket::error(string message)
{
cerr message;
}
void *TSocket::Try(void* arg)
{
int server_sock, clnt_sock;
struct sockaddr_in server_addr;
struct sockaddr_in clnt_addr;
int clnt_addr_size;
int cnt;
int sockfd, newsockfd, clilen;
pthread_t ClinConnThread;
C_Info clinInfo;
server_sock=socket(AF_INET, SOCK_STREAM, 0);
if(server_sock == -1)
error(socket() Error!);
//memset(&server_addr, 0, sizeof(server_addr));
bzero((char*) &server_addr, sizeof(server_addr) );
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(TcpPort);
if( bind(server_sock, (struct sockaddr*) &server_addr, sizeof(server_addr)) ==-1)
error(bind() Error!);
if( listen(server_sock, 5)==-1)
error(listen() Error!);
while(1)
{
clnt_addr_size = sizeof(clnt_addr);
newsockfd = accept(server_sock, (struct sockaddr *) &clnt_addr, (socklen_t*) &clnt_addr_size);
if (newsockfd 0) {
error(ERROR on TCP accept\n);
} else{
clinInfo.ClientIP = clnt_addr.sin_addr;
clinInfo.TSocket = newsockfd;
cout inet_ntoa(clnt_addr.sin_addr) is Connected endl;
pthread_create(&ClinConnThread, NULL, &TSocket::Connect, &clinInfo);
}
}
}
void *TSocket::Connect(void* ptr)
{
int server_sock, clnt_sock;
struct sockaddr_in server_addr;
struct sockaddr_in clnt_addr;
int clnt_addr_size;
char registed_id[] = hmi;
char registed_password[] = 1234;
char message[]=Connection complete;
char Valmsg[]= Welcome -HMI TEAM- ;
char Ivalmsg[]=TRY AGAIN!!;
char id[BUFSIZE];
char password[BUFSIZE];
int str_len;
char temp_id[100];
char temp_password[100];
C_Info* clinInfo = (C_Info*) ptr;// Parameter Processing
int NewSocket = clinInfo-TSocket;
//Send(Welcome TO HMI\n, NewSocket);
write(newsockfd, Welcome, length());
read(clnt_sock,id, BUFSIZE);
int a = strlen(id);
strncpy(temp_id,id, a );
int id_key = strcmp(registed_id,temp_id);
read(clnt_sock,password, BUFSIZE);
int b = strlen(password);
strncpy(temp_password,password, b );
int password_key = strcmp(registed_password,temp_password);
if ( id_key == 0 and password_key == 0)
{
cout Authentication Passed endl;
write(clnt_sock, Valmsg, sizeof(Valmsg));
}else{
cout Authentication Failed endl;
write(clnt_sock, Ivalmsg, sizeof(Ivalmsg));
}
//shutdown(NewSocket,SHUT_RD);
return 0;
}
int main()
{
pthread_t TcpTryThread;
pthread_create(&TcpTryThread, NULL, &TSocket::Try, NULL);
cout ************************************************* endl;
cout Welcome HMI Team endl;
cout TCP Port : TcpPort endl;
cout ************************************************* endl;
}
| 번호 | 제 목 | 글쓴이 | 날짜 |
|---|---|---|---|
| 2752085 | 문자열을 랜덤하게 출력해서 대체,삽입,삭제 횟수를 각가 표시하고 그걸 표 형태로 출력 | 슬아s | 2026-10-20 |
| 2752056 | 포인터 && 배열 질문요~ (2) | 해찬나래 | 2026-10-20 |
| 2752031 | x^n 구하는 코드 짜봤는데 값이 틀려요 ㅠㅠ | 세실 | 2026-10-20 |
| 2752000 | 나 정말 미치겠어요..ㅠㅜ (4) | 다이 | 2026-10-20 |
| 2751978 | 문자열 질문이요.. | 초시계 | 2026-10-19 |
| 2751945 | 숫자 맞추는 프로그램이요 ㅠㅠ (1) | 동은 | 2026-10-19 |
| 2751917 | C언어 이중 연결 관련 질문합니다. | 유우 | 2026-10-19 |
| 2751888 | return의 정확한 의미좀 알려주세요 (3) | 올해1살 | 2026-10-19 |
| 2751857 | 포인터 배열 (2) | 터큰 | 2026-10-18 |
| 2751831 | 연산자? 질문입니다 (2) | 진주 | 2026-10-18 |
| 2751800 | C언어 기초질문입니다.. (3) | 핫블랙 | 2026-10-18 |
| 2751739 | 파일 출력을 구조체로 받을 때, 파일의 끝을 판별하는 방법을 알려주세요. (2) | 얼 | 2026-10-17 |
| 2751715 | unresolved external symbol 에러... | 이룩 | 2026-10-17 |
| 2751686 | 변수 설정에 관한 질문입니다. | 꽃달 | 2026-10-17 |
| 2751655 | 자료구조 연습문제 | 한무릎 | 2026-10-17 |
| 2751631 | gets 함수 질문이요. (4) | 해늘 | 2026-10-16 |
| 2751553 | 퀵정렬 소스인데 주석좀 달아주세요 (1) | 싴흐한세여니 | 2026-10-16 |
| 2751528 | 저기 이거하는법좀 질문.. (2) | 가람슬기 | 2026-10-15 |
| 2751445 | 도저히 풀 수가 없네요.... (2) | 신당 | 2026-10-15 |
| 2751412 | 어제부터 계속 해도.........안되는.... (1) | 족장 | 2026-10-14 |
