// 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, clnt_addr;
int clnt_addr_size;
int err;
int ec;
struct data add;
sock = socket(AF_INET, SOCK_DGRAM, 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;
}
clnt_addr_size = sizeof(clnt_addr);
while(1) {
ec = recvfrom(sock, (char *)&add, sizeof(add), 0, (struct sockaddr *)&clnt_addr, &clnt_addr_size);
if(ec == SOCKET_ERROR) {
printf("recvfrom error\n");
break;
}
printf("Receive UDP message\n");
add.sum = add.a + add.b;
printf("%d + %d = %d\n", add.a, add.b, add.sum);
ec = sendto(sock, (char *)&add, sizeof(add), 0, (struct sockaddr *)&clnt_addr, sizeof(clnt_addr));
if(ec == SOCKET_ERROR) {
printf("sendto error\n");
break;
}
printf("send UDP message\n");
}
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;
struct data add;
add.sum = 0;
sock = socket(AF_INET, SOCK_DGRAM, 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));
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");
return FALSE;
}
while(1) {
printf("input A : ");
scanf("%d", &add.a);
printf("input B : ");
scanf("%d", &add.b);
ec = sendto(sock, (char *)&add, sizeof(add), 0, (struct sockaddr *) &serv_addr, serv_addr_len);
if(ec == SOCKET_ERROR) {
printf("sendto error\n");
break;
}
ec = recvfrom(sock, (char *)&add, sizeof(add), 0, (struct sockaddr *)&serv_addr, &serv_addr_len);
if(ec == SOCKET_ERROR) {
printf("recvfrom error\n");
break;
}
printf("Result : %d + %d = %d\n\n", add.a, add.b, add.sum);
}
closesocket(sock);
WSACleanup();
return 0;
}
'Programming > Socket' 카테고리의 다른 글
winsock TCP 덧셈 서버/클라이언트 (0) | 2007.09.14 |
---|---|
winsock 시작하기 (WSAStartup, WSACleanup) (0) | 2007.09.12 |
Linux Socket Programming 예제 (0) | 2007.09.10 |