Bu sınıfta veriler alındığı için en son olarak burda kaç ms de geldiğinin hesabı yapılmaktadırSoket programlama UDP port ile yazılmıştır.Server verinin alındığı kısım denmektedir.Client ise verinin iletildiği yerdir.
1.Senderdır(Server)Bu kısımda veriler sendto fonksiyonuyla yollanmaktadır.
2. Serverdır(Client )Bu kısımda veriler
hem alınmakta hemde gönderilmektedir.Recvfrom ve sendto
3.Recevier(Client)
Bu kısımda ise Serverdan gelen bilgiler alınmaktadır.Burda
verinin kaç saniyede geldiği ve kaç eksik paket bulunduğu yazılacaktır.
Program cmd de şu şekilde açılmaktadır.Üç adet cmd açılacaktır.
Desktop\NetSendReceive\Debug\Receiver.exe 2000 0
Desktop\NetSendReceive\Debug\Server.exe 1000 127.0.0.1 2000
Desktop\NetSendReceive\Debug\Sender.exe 127.0.0.1 1000 0
Dosya uzantınız nerdeyse ona göre cmd' ye bildirmelisiniz .Ben kendi uzantı adresime göre vermiş bulunmaktayım.
Receiver Sınıfı
// ReceivingClient.cpp : Defines the entry point for the console application.
//
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <Ws2tcpip.h>
#include <time.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define BUFLEN 8192
int main(int argc, char *argv[])
{
struct sockaddr_in si_other;
int s;
int slen;
int iTotalBytes = 0;
int iRecvBytes;
int bFirstDataFlag = 0;
int const buff_size = 8 * 1024 * 1024; // 8 MB ethernet buffer
int iResult;
int iloopcount = 1;
int iLostPackets = 0;
int p = 0;
int PackNum = 1;
char buf[BUFLEN];
u_long iMode = 0;
LARGE_INTEGER EndingTime;
LARGE_INTEGER ElapsedMicroseconds;
LARGE_INTEGER Frequency;
LONGLONG StartTime;
WSADATA wsa;
if (argc != 2)
{
printf("Usage: receiver <rcv port>\n");
_getch();
exit(1);
}
//Initialise winsock
printf("Initialising Winsock...\n");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("Failed. Error Code : %d\n", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialised.\n");
//create socket
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == SOCKET_ERROR)
{
printf("socket() failed with error code : %d\n", WSAGetLastError());
exit(EXIT_FAILURE);
}
setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)(&buff_size), sizeof(buff_size));
iResult = ioctlsocket(s, FIONBIO, &iMode);
if (iResult != NO_ERROR)
{
printf("ioctlsocket failed with error: %ld\n", iResult);
}
//setup address structure
memset((char *)&si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(atoi(argv[1]));
si_other.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
//Bind Socket
if (bind(s, (struct sockaddr *)&si_other, sizeof(si_other)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Bind done.\n");
slen = sizeof(si_other);
memset(buf, 0, BUFLEN);
QueryPerformanceFrequency(&Frequency);
while (iloopcount > 0)
{
iRecvBytes = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen);
if (iRecvBytes == SOCKET_ERROR)
{
if (WSAGetLastError() == 10035)
{
continue;
}
printf("recvfrom() failed with error code : %d", WSAGetLastError());
_getch();
exit(EXIT_FAILURE);
}
else
{
if (bFirstDataFlag == 0)
{
iloopcount = *((int *)buf);
StartTime = *((LONGLONG*)(buf + sizeof(int)));
bFirstDataFlag = 1;
}
// Use below to verify each packet is received
PackNum = *((int*)(buf + sizeof(int) + sizeof(LONGLONG)));
iloopcount--;
p++;
if (p != PackNum)
{
//printf("p: %d Packnum: %d\n", p, PackNum);
iLostPackets += (PackNum - p);
iloopcount -= (PackNum - p);
p = PackNum;
}
//printf(".");
//printf("%d %d %d %d\n", p, iloopcount, iRecvBytes, PackNum);
//printf("%d\n", PackNum);
}
}
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartTime;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
printf("\nReceived %d bytes in %d packets in %d milliseconds.", p * iRecvBytes, p, ElapsedMicroseconds.QuadPart / 1000);
printf("\nLost packets: %d", iLostPackets);
printf("\nSpeed: %f Megabits per sec", (float)(8 * p * iRecvBytes) / (float)ElapsedMicroseconds.QuadPart);
printf("\nPress enter.\n");
_getch();
return 0;
}
Sender Sınıfı
// SendingClient.cpp : Defines the entry point for the console application.
//
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <Ws2tcpip.h>
#include <time.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define BUFLEN 5000
#define DATASIZE 1024
#define LOOPCOUNT 1024
void initBuffer(char **pBuf, int nSize, int loopcount)
{
char ch = 'x';
int datasize;
datasize = nSize;
nSize += (sizeof(int) + sizeof(LONGLONG) + 1 + sizeof(int));
*pBuf = (char *)malloc(nSize);
memset(*pBuf, 0, nSize);
memset((*pBuf + sizeof(int) + sizeof(LONGLONG) + sizeof(int)), ch, datasize);
*((int*)*pBuf) = loopcount;
}
int main(int argc, char *argv[])
{
struct sockaddr_in si_other;
int s;
int slen = sizeof(si_other);
int count;
int nSize = 10;
int loopcount;
int p;
int bFirstDataFlag = 0;
char *pBuffer;
WSADATA wsa;
LARGE_INTEGER StartTime;
LARGE_INTEGER EndTime;
LARGE_INTEGER Frequency;
LARGE_INTEGER ElapsedMicroseconds;
LONGLONG a;
if ((argc >= 3) && (argc <= 5))
{
for (count = 1; count < argc; count++)
{
printf("argv[%d] = %s\n", count, argv[count]);
}
}
else
{
printf("Usage: sender <sending ip> <send port> [data package size] [loop count]\n");
_getch();
exit(1);
}
//Initialise winsock
printf("Initialising Winsock...\n");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("Failed. Error Code : %d\n", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialised.\n");
//create socket
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == SOCKET_ERROR)
{
printf("socket() failed with error code : %d\n", WSAGetLastError());
exit(EXIT_FAILURE);
}
//setup address structure
memset((char *)&si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(atoi(argv[2]));
si_other.sin_addr.S_un.S_addr = inet_addr(argv[1]);
if (argc >= 4)
{
nSize = atoi(argv[3]);
}
else
{
nSize = DATASIZE;
}
if (argc == 5)
{
loopcount = atoi(argv[4]);
}
else
{
loopcount = LOOPCOUNT;
}
initBuffer(&pBuffer, nSize, loopcount);
slen = sizeof(si_other);
p = 1;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&StartTime);
//start communication
while (loopcount > 0)
{
if (bFirstDataFlag == 0)
{
a = StartTime.QuadPart;
memcpy((pBuffer + sizeof(int)), &a, sizeof(a));
bFirstDataFlag = 1;
}
memcpy((pBuffer + sizeof(int) + sizeof(LONGLONG)), &p, sizeof(p));
//send the message
if (sendto(s, pBuffer, nSize, 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
{
printf("sendto() failed with error code : %d", WSAGetLastError());
_getch();
exit(EXIT_FAILURE);
}
else
{
p++;
loopcount--;
//printf(".");
}
}
QueryPerformanceCounter(&EndTime);
ElapsedMicroseconds.QuadPart = EndTime.QuadPart - StartTime.QuadPart;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
printf("\nSent %d bytes in %d packets in %d milliseconds.", (p - 1) * nSize, p - 1, ElapsedMicroseconds.QuadPart / 1000);
printf("\nPress enter.\n");
_getch();
return 0;
}
Server Sınıfı
// server.cpp : Defines the entry point for the console application.
//
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <conio.h>
#include <stdio.h>
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <time.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define BUFLEN 5000
int main(int argc, char *argv[])
{
int count;
int n;
if (argc > 3)
{
for (count = 1; count < argc; count++)
{
printf("argv[%d] = %s\n", count, argv[count]);
}
}
else
{
printf("Usage: server <rcv port> <sending ip> <send port 1> ... <send port n>\n");
getchar();
exit(1);
}
int SockNum = count - 3;
printf("SockNum = %d\n", SockNum);
SOCKET s;
struct sockaddr_in server, si_other;
struct sockaddr_in si_other2[10];
int slen;
int recv_len;
char buf[BUFLEN];
WSADATA wsa;
slen = sizeof(struct sockaddr_in);
//Initialise winsock
printf("Initialising Winsock...\n");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("Failed. Error Code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialised.\n");
//Create ReceivingSocket
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
{
printf("Could not create ReceivingSocket : %d", WSAGetLastError());
}
printf("ReceivingSocket created.\n");
//Configure ReceivingSocket
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(atoi(argv[1]));
SOCKET SocketArray[10];
for (n = 1; n <= SockNum; n++)
{
//Create SendingSockets
if ((SocketArray[n] = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
{
printf("Could not create SendingSocket : %d", WSAGetLastError());
}
printf("SendingSocket %d created.\n", n);
//Configure SendingSockets
si_other2[n].sin_family = AF_INET;
si_other2[n].sin_addr.s_addr = inet_addr(argv[2]);
si_other2[n].sin_port = htons(atoi(argv[n + 2]));
}
//Bind ReceivingSocket
if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d", WSAGetLastError());
_getch();
exit(EXIT_FAILURE);
}
printf("Bind done.\n");
//clear the buffer
memset(buf, '\0', BUFLEN);
printf("Waiting for data...\n");
//Listening
while (1)
{
// receive data from SendingClient
if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == SOCKET_ERROR)
{
printf("recvfrom() failed with error code : %d", WSAGetLastError());
break;//exit(EXIT_FAILURE);
}
else
{
//printf(".");
for (n = 1; n <= SockNum; n++)
{
//send data to ReceivingClient
if (sendto(SocketArray[n], buf, recv_len, 0, (struct sockaddr*) &si_other2[n], slen) == SOCKET_ERROR)
{
printf("sendto() failed with error code : %d", WSAGetLastError());
}
}
}
}
return 0;
}
Hiç yorum yok:
Yorum Gönder