2011. 9. 3. 14:53


/*
2011.05.17.-18.
Daun.
1:1chatting.
Server..
*/
#include <winsock2.h>
#include <stdio.h>
#include<iostream>

#include<process.h> //스레드
#include<windows.h> //스레드

#include<time.h> //sleep 사용시

 


#define PORT 3000;
 char buf[256]={0};
 SOCKET soc, soc_client;


unsigned WINAPI rThreadFunction(void* arg);


unsigned WINAPI sThreadFunction(void* arg);

 

void main()
{
 WSADATA wsa;


 struct sockaddr_in addr;
 int size;

 HANDLE rThread, sThread;

 int intThread=0;
 WSAStartup(MAKEWORD(2,0),&wsa);

 soc = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

 

 addr.sin_family=AF_INET;
 addr.sin_port=htons(3000);
 addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);

 bind(soc,(struct sockaddr*)&addr, sizeof(addr));

 listen(soc, 5);

 size = sizeof(addr);

 soc_client = accept(soc, (struct sockaddr*)&addr, &size);


 while(1)
 {
 rThread=(HANDLE)_beginthreadex(0,0,rThreadFunction,0,0,(unsigned*)intThread);
 sThread=(HANDLE)_beginthreadex(0,0,sThreadFunction,0,0,(unsigned*)intThread);
}
 TerminateThread(rThread,0);
 TerminateThread(sThread,0);

 

 closesocket(soc);
 WSACleanup();
}

unsigned WINAPI rThreadFunction(void* arg)
{
 for(int i=1;i<=30;i++)
 {
 recv(soc_client, buf, 256,0);
 printf("상대방 : %s\n",buf);
 }
 return 0;
}


unsigned WINAPI sThreadFunction(void* arg)
{
 for(int i=1;i<=30;i++)
 {
  
 scanf("%s",&buf);
 send(soc_client, buf, 256, 0);


 }
 return 0;
}

'Code > socket' 카테고리의 다른 글

[SOCKET] clinet for poker  (0) 2011.09.03
[SOCKET]1:1 chating client  (0) 2011.09.03
[SOCKET]chating server for test  (0) 2011.09.03
[SOCKET]chating client for test  (0) 2011.09.03
Posted by I_co