=== UDP 서버
#include "stdafx.h"
#pragma comment(lib, "ws2_32.lib")
#include <winsock2.h>
#include <stdio.h>

#define PORT 65000
#define BUFMAX 255

struct dataStruct
{
    int intValue;
    char charValue[100];
    float floatValue;
};

void ErrorMessage(char *errorMessage)  /* External error handling function */
{
    printf("%s\n", errorMessage);
    exit(1);
}

int _tmain(int argc, _TCHAR* argv[])
{
    WSADATA wsaData;               

    SOCKET sock;
    SOCKADDR_IN echoServAddr;
    SOCKADDR_IN echoClntAddr;
                        
    if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) /* Load Winsock 2.0 DLL */
    {
        fprintf(stderr, "WSAStartup() failed");
        exit(1);
    }

    /* Create socket for sending/receiving datagrams */
    if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
        ErrorMessage("socket() failed");

    /* Construct local address structure */
    memset(&echoServAddr, 0, sizeof(echoServAddr));
    echoServAddr.sin_family = AF_INET;
    echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    echoServAddr.sin_port = htons(PORT);

    /* Bind to the local address */
    if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) != 0)
        ErrorMessage("bind() failed");

    for (;;) /* Run forever */
    {
        char echoBuffer[BUFMAX];
        int cliLen = sizeof(echoClntAddr);
        int recvMsgSize = recvfrom(sock, echoBuffer, BUFMAX, 0, (struct sockaddr*) &echoClntAddr, &cliLen);
        if (recvMsgSize < 0)
            ErrorMessage("recvfrom() failed");

        dataStruct recvData;
        memcpy(&recvData, echoBuffer, sizeof(recvData));
        printf("RecvData - %d, %s, %f\n", recvData.intValue, recvData.charValue, recvData.floatValue);

        dataStruct sendData;
        ZeroMemory(&sendData, sizeof(sendData));
        sendData.intValue = 101;
        sprintf(sendData.charValue, "잘받음");
        sendData.floatValue = 12.11f;

        char sendBuf[BUFMAX];
        ZeroMemory(sendBuf, sizeof(sendBuf));
        memcpy(sendBuf, &sendData, sizeof(sendData));

        if (sendto(sock, sendBuf, sizeof(sendData), 0, (struct sockaddr *) &echoClntAddr, sizeof(echoClntAddr)) == 0)
        {
            ErrorMessage("sendto() send Error");
        }
    }

    return 0;
}
=== php 클라이언트
<?php
    class CDataClass
    {
        private $intValue;
        private $charValue;
        private $floatValue;
       
        public function SetDataInt($int)
        {
            $this->intValue = $int;
        }
       
        public function &GetDataInt()
        {
            return $this->intValue;
        }
       
        public function SetDataChar($char)
        {
            $this->charValue = $char;
        }
       
        public function &GetDataChar()
        {
            return $this->charValue;
        }
       
        public function SetDataFloat($float)
        {
            $this->floatValue = $float;
        }
       
        public function &GetDataFloat()
        {
            return $this->floatValue;
        }
       
        public function PackData()
        {
            $temp = pack("i", $this->intValue);
            $temp .= pack("a100", $this->charValue);
            $temp .= pack("f", $this->floatValue);

            return $temp;
        }
       
        public function UnpackData($value)
        {
            $array = unpack("iInt/a100Char/fFloat", $value);
           
            $this->intValue = $array[Int];
            $this->charValue = $array[Char];
            $this->floatValue = $array[Float];
        }
    }

define("_IP",    "127.0.0.1");
define("_PORT",  "65000");

$sendData = new CDataClass();
$sendData->SetDataInt(100);
$sendData->SetDataChar("아햏햏");
$sendData->SetDataFloat(10.1);

$temp1 = $sendData->GetDataInt();
$temp2 = $sendData->GetDataChar();
$temp3 = $sendData->GetDataFloat();
echo "CLIENT >> send Data - $temp1 , $temp2 , $temp3 
"; $sendbuf = $sendData->PackData(); $sendlen = strlen($sendbuf); $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_sendto($sock, $sendbuf, $sendlen, 0, _IP, _PORT); $from = ''; $port = 0; $recvSize = socket_recvfrom($sock, $buf, 4096, 0, $from, $port); $recvData = new CDataClass(); $recvData->UnpackData($buf); $temp1 = $recvData->GetDataInt(); $temp2 = $recvData->GetDataChar(); $temp3 = $recvData->GetDataFloat(); socket_close($sock); echo "CLIENT >> recv Data - $temp1 , $temp2 , $temp3
"; ?>
by 널부러 2013. 10. 1. 18:46