moj problem polaga na tym ze zrobilem program client -server za pomoca protokolu TCP. wszystko pieknie dziala wysyla i odbiera wszystko. ale chcialbym zrobic aby laczyl sie teraz za pomoca protokolu UDP. czy z pomizej podane kodu server - klient o protokole TCP. da sie go zmienic na protokol UDP. kazda pomoc mile widziana.

//klient

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <conio.h>

int main(int argc, char *argv)
{
WORD RequiredVersion;
WSADATA WData;
SOCKET s;
struct sockaddr_in addr;
struct hostent
he;
typedef struct
{
char nick[20];
char adresat[20];
char text[255];
}
PAKIET;
PAKIET wsk;
printf("Ladowanie winsock...\n\n");
RequiredVersion = MAKEWORD(2, 0);
if (WSAStartup(RequiredVersion, &WData) != 0)
{
printf("Blad WinSock\n");
return 1;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(1);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
printf("Jesli jest wlaczony serwer wcisnij |ENTER|");
gets(wsk->nick);
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
printf("\nNawiazuje polaczenie z serwerem...\n");
connect(s, (struct sockaddr
)&addr, sizeof(addr));
printf("\nNawiazano polaczenie...\n");
while(true)
{
printf(">");
gets(wsk->text);
send(s,(char*)wsk ,sizeof(PAKIET),MSG_OOB);
}
closesocket(s);
WSACleanup();
return 0;
}

//server

#include <stdio.h>
#include <iostream.h>
#include <winsock2.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>

char *Buffer = NULL;
unsigned len = 256;
typedef struct
{
char nick[20];
char adresat[20];
char text[255];
}
PAKIET;
PAKIET paczka;

#define LOG(msg) printf("\n| %s |\n",msg)

SOCKET SetUpSocket(char Address, int Port)
{
unsigned long HandleAddr = inet_addr(Address);
if (HandleAddr != INADDR_NONE)
{
SOCKET ss = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ss != INVALID_SOCKET)
{
sockaddr_in Addr;
Addr.sin_family = AF_INET;
Addr.sin_addr.s_addr = HandleAddr;
Addr.sin_port = Port;
if (bind(ss, (sockaddr
)&Addr, sizeof(sockaddr_in)) != SOCKET_ERROR)
{ listen(ss, 1); return ss; };
}
}
return INVALID_SOCKET;
}
SOCKET AcceptConnection(SOCKET MainSocket, sockaddr_in &RemoteAddr)
{
int AddrSize = sizeof(RemoteAddr);
return accept(MainSocket, (sockaddr)&RemoteAddr, &AddrSize);
}
unsigned GetBuffer(SOCKET ss)
{
int ReadBytes = 0;
do
{
ReadBytes = recv(ss, (char
)&paczka, sizeof(PAKIET), 0);
if (ReadBytes > 0)
{
printf("%s\n",paczka.text);
}
}
while (ReadBytes > 0);
return 0;
}
int wsInit()
{
WORD RequiredVersion;
WSADATA WData;
RequiredVersion = MAKEWORD(2, 0);
if (WSAStartup(RequiredVersion, &WData) != 0)
{
return -1;
}
if(!Buffer)
Buffer = new char[len];
return 0;
}
void wsFree()
{
WSACleanup();
if(Buffer)
delete Buffer;
Buffer = NULL;
}
int main()
{
if(wsInit() != 0) return -1;
char Adres = "127.0.0.1";
int Port = 1;
SOCKET Socket = SetUpSocket(Adres, htons(Port));
if(Socket == INVALID_SOCKET)
{
return -1;
}
while (true)
{
LOG("Prosze czekac na polaczenie z klientem...");
sockaddr_in RemoteAddr;
SOCKET ss = AcceptConnection(Socket, RemoteAddr);
char
RemoteAddress = NULL;
int RemotePort = 0;
if (ss != INVALID_SOCKET)
{
LOG("Akceptacja polaczenie");
RemoteAddress = inet_ntoa(RemoteAddr.sin_addr);
RemotePort = ntohs(RemoteAddr.sin_port);
}
GetBuffer(ss);
}
wsFree();
cin.get();
}