// API_test.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>
#pragma comment (lib, "ws2_32.lib") //수동으로 lib 추가하기
struct data
{
int a;
int b;
int sum;
};
int main(int argc, char* argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2,2); //버젼 2.2 사용지정
if(WSAStartup(wVersionRequested, &wsaData) != 0) //WSAStartup 부르기
{
return FALSE;
}
if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
WSACleanup();
return FALSE;
}
printf("WSAStartip success...\n");
SOCKET sock, con_sock;
struct sockaddr_in serv_addr, clnt_addr;
int clnt_addr_size;
int err;
int ec;
struct data add;
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == INVALID_SOCKET) {
err = WSAGetLastError();
printf("error: %s\n", err);
return FALSE;
}
printf("socket success...\n");
memset(&serv_addr, 0, sizeof(serv_addr)); //내꺼 우선 0으로 초기화
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //서버는 INADDR_ANY사용
serv_addr.sin_port = htons(9090);
ec = bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if (ec == SOCKET_ERROR) {
printf("bind error\n");
return FALSE;
}
ec = listen(sock, SOMAXCONN);
if (ec == SOCKET_ERROR) {
printf("listen error\n");
return FALSE;
}
clnt_addr_size = sizeof(clnt_addr);
while(1) {
con_sock = accept(sock, (struct sockaddr *)&clnt_addr, &clnt_addr_size);
if (con_sock != INVALID_SOCKET) {
while(1) {
ec = recv(con_sock, (char *)&add, sizeof(add), 0);
if(ec == SOCKET_ERROR) {
printf("recv error\n");
closesocket(con_sock);
break;
}
printf("Rece TCP message\n");
add.sum = add.a + add.b;
printf("%d + %d = %d\n", add.a, add.b, add.sum);
ec = send(con_sock, (char *)&add, sizeof(add), 0);
if(ec == SOCKET_ERROR) {
printf("send error\n");
closesocket(con_sock);
break;
}
printf("send TCP message\n\n");
}
}
else {
closesocket(sock);
WSACleanup();
return 0;
}
}
}
// API_test.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>
#pragma comment (lib, "ws2_32.lib") //수동으로 lib 추가하기
struct data
{
int a;
int b;
int sum;
};
int main(int argc, char* argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2,2); //버젼 2.2 사용지정
if(WSAStartup(wVersionRequested, &wsaData) != 0) //WSAStartup 부르기
{
return FALSE;
}
if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
WSACleanup();
return FALSE;
}
printf("WSAStartip success...\n");
SOCKET sock;
struct sockaddr_in serv_addr;
int serv_addr_len;
int err;
int ec = 0;
struct data add;
add.sum = 0;
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == INVALID_SOCKET) {
err = WSAGetLastError();
if (ec == WSAECONNREFUSED) {
printf("connect refused\n");
}
else if (ec == WSAETIMEDOUT) {
printf("connedt time out\n");
}
closesocket(sock);
return FALSE;
}
printf("socket success...\n");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET; //IPv4
serv_addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //클라는 상대방 주소를 알아야한다.
serv_addr.sin_port = htons(9090);
serv_addr_len = sizeof(serv_addr);
ec = connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if (ec == SOCKET_ERROR) {
printf("connect error\n");
closesocket(sock);
return FALSE;
}
while(1) {
printf("input A : ");
scanf("%d", &add.a);
printf("input B : ");
scanf("%d", &add.b);
ec = send(sock, (char *)&add, sizeof(add), 0);
if(ec == SOCKET_ERROR) {
printf("send error\n");
closesocket(sock);
break;
}
ec = recv(sock, (char *)&add, sizeof(add), 0);
if(ec == SOCKET_ERROR) {
printf("recv error\n");
closesocket(sock);
break;
}
printf("Result : %d + %d = %d\n\n", add.a, add.b, add.sum);
}
closesocket(sock);
WSACleanup();
return 0;
}
'Programming > Socket' 카테고리의 다른 글
winsock UDP 덧셈 서버/클라이언트 (0) | 2007.09.13 |
---|---|
winsock 시작하기 (WSAStartup, WSACleanup) (0) | 2007.09.12 |
Linux Socket Programming 예제 (0) | 2007.09.10 |