반응형
//실습 2번 #include#include #include #define BUFSIZE 512 //실습4번 int main(int argc, char* argv[]) { WSADATA wsa; if(WSAStartup(MAKEWORD(2,2),&wsa) != 0) return -1; char *ipaddr ="147.46.114.70"; printf("IP주소 = %s \n",ipaddr); printf("변환후 = 0x%x\n",inet_addr(ipaddr)); IN_ADDR temp; temp.s_addr = inet_addr(ipaddr); printf("변환 = %s\n", inet_ntoa(temp)); WSACleanup(); return 0; } /* void err_quit(char *msg) ; void err_display(char *msg); int main(int argc, char* argv[]) { int retval; WSADATA wsa; if(WSAStartup(MAKEWORD(2,2), &wsa) != 0) return -1; SOCKET listen_sock = socket(AF_INET, SOCK_STREAM, 0); if(listen_sock == INVALID_SOCKET) err_quit("socket()"); SOCKADDR_IN serveraddr; ZeroMemory(&serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_port = htons(9000); serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); retval = bind(listen_sock, (SOCKADDR *)&serveraddr, sizeof(serveraddr)); if(retval == SOCKET_ERROR) err_quit("bind()"); retval = listen(listen_sock, SOMAXCONN); if(retval == SOCKET_ERROR) err_quit("listen()"); SOCKET client_sock; SOCKADDR_IN clientaddr; int addrlen; char buf[BUFSIZE+1]; while(1) { addrlen = sizeof(clientaddr); client_sock = accept(listen_sock, (SOCKADDR *)&clientaddr, &addrlen); if(client_sock == INVALID_SOCKET) { err_display("accept()"); continue; } printf("\n[TCP 서버] 클라이언트접속: IP 주소=%s, 포트、번호=%d\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port)); while(1) { retval = recv(client_sock, buf, BUFSIZE, 0); if(retval == SOCKET_ERROR) { err_display("recv()"); break; } else if(retval == 0) { break; } else { buf[retval] = '\0'; printf("%s", buf); } //보내기 retval = send(client_sock, strupr(buf), BUFSIZE, 0); if( retval== SOCKET_ERROR){ err_display("send()"); break; } printf("[TCP 클라이언트] %d 바이트를 보냈습니다.\n",retval); } closesocket(client_sock); printf("\n[TCP 서버클라이언트 종료: IP 주소=%s, 포트번호=%d\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port)); } closesocket(listen_sock); WSACleanup(); return 0; } void err_quit(char *msg) { LPVOID ipMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, WSAGetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&ipMsgBuf,0,NULL); MessageBox(NULL, (LPCTSTR)ipMsgBuf, msg, MB_ICONERROR); LocalFree(ipMsgBuf); exit(-1); } void err_display(char *msg) { LPVOID ipMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, WSAGetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&ipMsgBuf,0,NULL); printf("[%s] %s", msg, (LPCTSTR)ipMsgBuf); LocalFree(ipMsgBuf); }*/ ////////////////////////// /* #include #include #include #define BUFSIZE 512 int recvn(SOCKET s, char *buf, int len, int flags); void err_quit(char *msg); void err_display(char *msg); int main(int argc, char* argv[]) { int retval; WSADATA wsa; if(WSAStartup(MAKEWORD(2,2),& wsa) !=0) return -1; SOCKET sock = socket(AF_INET, SOCK_STREAM, 0); if(sock == INVALID_SOCKET) err_quit("socket()"); SOCKADDR_IN serveraddr; ZeroMemory(&serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET;r serveraddr.sin_port = htons(9000); serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1"); retval - connect(sock, (SOCKADDR *)&serveraddr, sizeof(serveraddr)); if(retval == SOCKET_ERROR) err_quit("connect()"); char buf[BUFSIZE+1]; int len; while(1) { ZeroMemory(buf, sizeof(buf)); printf("\n[보낼 테이터]"); if(fgets(buf, BUFSIZE+1, stdin) == NULL) break; // \n 문자제거 len =strlen(buf); if(buf[len-1] == '\n') buf[len-1] = '\0'; if(strlen(buf) == 0) break; retval = send(sock, buf, strlen(buf), 0); if( retval== SOCKET_ERROR){ err_display("send()"); break; } printf("[TCP 클라이언트] %d 바이트를 보냈습니다.",retval); retval = recvn(sock, buf, retval, 0); if(retval == SOCKET_ERROR) { err_display("recv()"); break; } else if(retval == 0) break; buf[retval] = '\0'; printf("[TCP 클라이언트] %d바이트를 받았습니다", retval); printf("[받은 데이터]%s\n", buf); } closesocket(sock); WSACleanup(); return 0; } //리시브 int recvn(SOCKET s, char *buf, int len, int flags) { int received; char *ptr = buf; int left = len; while(left >0){ received =recv(s,ptr,left,flags); if(received == SOCKET_ERROR) return SOCKET_ERROR; else if(received == 0) break; left -= received; ptr += received; } return (len - left); } void err_quit(char *msg) { LPVOID ipMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, WSAGetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&ipMsgBuf,0,NULL); MessageBox(NULL, (LPCTSTR)ipMsgBuf, msg, MB_ICONERROR); LocalFree(ipMsgBuf); exit(-1); } void err_display(char *msg) { LPVOID ipMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, WSAGetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&ipMsgBuf,0,NULL); printf("[%s] %s", msg, (LPCTSTR)ipMsgBuf); LocalFree(ipMsgBuf); }*/
'C, C++' 카테고리의 다른 글
[C++] C++ 의 구조와 C언어 개선사항 (0) | 2014.11.18 |
---|---|
[C] 셀렉트를 사용한 통신 (0) | 2014.11.18 |
[C] 소켓 공부하기!! (0) | 2014.11.18 |
[C] [자료구조] fib 함수를 사용해서 순환호출를 이해하기 (0) | 2014.11.18 |
[C] 자료구조 순환적인 프로그램 (0) | 2014.11.18 |