I am testing differnent Model of using socket communiton. In my opinion, there are many ways we can use. Now i will test different communication model . I will try my best to samplified it. One way is as follows:
Server:
::CSocket S,S2;
::SOCKADDR sr ;
S.Create(1088);
S.Bind(1088,L"192.168.0.40");
S.Listen(6);
S.Accept(S2,&sr);
char p[]="ASDF";
S2.Send(L"dddddd",14);
Client:
::CSocket s;
s.Create();
s.Connect(L"192.168.0.40",1088);
WCHAR buf[32] = { 0 };
s.Receive(buf,32);
me.SetWindowText(buf);
You can use socket achieving this aim :
Client:
// TServer.cpp : Defines the entry point for the console application.
// socket server
#include "stdafx.h"
#include <WINSOCK2.H>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main(int argc, char* argv[])
{
WORD uVersionInfo;
WSADATA wdata;
uVersionInfo = MAKEWORD(1,1);
if(!WSAStartup(uVersionInfo,&wdata))
cout<<"init socket lib success"<<endl;
if(LOBYTE(wdata.wVersion) != 1 || HIBYTE(wdata.wVersion) != 1)
cout<<"version error"<<endl;
SOCKET s = socket(AF_INET,SOCK_DGRAM, 0);
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.S_un.S_addr = inet_addr("192.168.0.40");
sin.sin_port = htons(1088);
// bind(s, (struct sockaddr*)&sin , sizeof(sin));
//listen(s,5);
char buf[10] = "abc";
sockaddr sa;
int len = sizeof(sin);
// int count = recvfrom(s, buf, 10, 0, &sa, &len);
int count = sendto(s, buf, 10, 0, (struct sockaddr*)&sin, len);
cout<<"sended num :"<<count<<endl;
closesocket(s);
WSACleanup();
return 0;
}
Server :
// TServer.cpp : Defines the entry point for the console application.
// socket server
#include "stdafx.h"
#include <WINSOCK2.H>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
WORD uVersionInfo;
WSADATA wdata;
uVersionInfo = MAKEWORD(1,1);
if(!WSAStartup(uVersionInfo,&wdata))
cout<<"init socket lib success"<<endl;
if(LOBYTE(wdata.wVersion) != 1 || HIBYTE(wdata.wVersion) != 1)
cout<<"version error"<<endl;
SOCKET s = socket(AF_INET,SOCK_DGRAM, 0);
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.S_un.S_addr = inet_addr("192.168.0.40");
sin.sin_port = htons(1088);
bind(s, (struct sockaddr*)&sin , sizeof(sin));
//listen(s,5);
char buf[10];
sockaddr sa;
int len = sizeof(sa);
int count = recvfrom(s, buf, 10, 0, &sa, &len);
cout<<"receive num :"<<count<<"mes"<<buf<<endl;
closesocket(s);
WSACleanup();
return 0;
}