TCP-IP 소켓통신 THREAD CHAPTER3

    TCP-IP 소켓통신 THREAD CHAPTER3

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <pthread.h>
    void* thread_main(void *arg);
    
    int main(int argc, char *argv[])
    {
    	pthread_t t_id;
    	int thread_param = 5;
    	void *thr_ret;
    
    	if(pthread_create(&t_id, NULL, thread_main, (void*)&thread_param)!=0)
    	{
    		puts("pthread_create() error");
    		return -1;
    	};
    	
    	if(pthread_join(t_id, &thr_ret)!=0)
    	{
    		puts("pthread_join() error");
    		return -1;
    	};
    
    	printf("Thread return message : %s \n", (char*)thr_ret);
    	free(thr_ret);
    	return 0;
    }
    
    void* thread_main(void *arg)
    {
    	int i;
    	int cnt = *((int*)arg);
    	char *msg=(char *)malloc(sizeof(char)*50);
    	strcpy(msg, "Hello, I'am thread~ \n");
    	
    	for(i=0; i<cnt; i++)
    	{
    		sleep(1); puts("running thread");
    	}
    	return (void*)msg;
    }

    '코딩 > TCP&IP' 카테고리의 다른 글

    소켓 통신 TCP-IP INET_ADDR  (0) 2019.12.06
    TCP-IP 소켓통신 THREAD CHAPTER4  (0) 2019.12.04
    TCP-IP 소켓통신 THREAD CHAPTER2  (0) 2019.12.02
    TCP-IP 소켓통신 THREAD CHAPTER1  (0) 2019.12.01
    news_receiver 소켓통신 소스코드  (0) 2019.11.28

    댓글

    Designed by JB FACTORY