Torna indietro   Hardware Upgrade Forum > Software > Programmazione

AMD Ryzen 5 7500X3D: la nuova CPU da gaming con 3D V-Cache per la fascia media
AMD Ryzen 5 7500X3D: la nuova CPU da gaming con 3D V-Cache per la fascia media
Vediamo come si comporta il Ryzen 5 7500X3D, nuovo processore di casa AMD che fonde 6 core Zen 4 con la tecnologia 3D V-Cache, particolarmente utile in scenari come il gaming. Annunciato a un prezzo di listino di 279€, il nuovo arrivato sarà in grado di diventare un riferimento per i sistemi budget? Ecco cosa ne pensiamo.
SONY BRAVIA 8 II e BRAVIA Theatre System 6: il cinema a casa in formato compatto
SONY BRAVIA 8 II e BRAVIA Theatre System 6: il cinema a casa in formato compatto
Bravia 8 II rinnova l’eredità dell’A95L con maggiore luminosità e colori più precisi. Il taglio da 55” offre un’esperienza cinematografica immersiva anche in spazi ridotti, amplificata dalla soundbar Sony Theatre System 6
KTC H27E6 a 300Hz e 1ms: come i rivali ma a metà prezzo
KTC H27E6 a 300Hz e 1ms: come i rivali ma a metà prezzo
KTC lancia il nuovo monitor gaming H27E6, un modello da 27 pollici che promette prestazioni estreme grazie al pannello Fast IPS con risoluzione 2K QHD (2560x1440). Il monitor si posiziona come una scelta cruciale per gli appassionati di eSport e i professionisti creativi, combinando una frequenza di aggiornamento di 300Hz e un tempo di risposta di 1ms con un'eccezionale fedeltà cromatica
Tutti gli articoli Tutte le news

Vai al Forum
Rispondi
 
Strumenti
Old 27-09-2004, 11:27   #1
Rigo007
Member
 
L'Avatar di Rigo007
 
Iscritto dal: Oct 2001
Città: Trento
Messaggi: 164
echo server/client UDP in MVC++

ciao! Devo fare un echo server e un client ke comunicano tra loro usando il protocollo UDP!
Questo protocollo usa le funzioni sendto() e recvfrom()...
Io ho scritto questo codice...qualcuno riesce a sistemarmelo utilizzando tali funzioni?
Ho inserito nel codice dei commenti e degli esempi ke potrebbero essere utili (o fastidiosi), vedete vuoi!

Thank you...anticipatamente!

Nb. per compilare dovete inserire nel linker la libreria wsock32.lib
Rigo007 è offline   Rispondi citando il messaggio o parte di esso
Old 27-09-2004, 11:29   #2
Rigo007
Member
 
L'Avatar di Rigo007
 
Iscritto dal: Oct 2001
Città: Trento
Messaggi: 164
// UDP client using Winsock 2.0

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

#include <string>
#include <iostream>

using namespace std ;



#define DEFAULT_PORT 5001

int main() {
char Buffer[128];
char *server_name = "localhost";
unsigned short port = DEFAULT_PORT;

//
char ch;
string s;
//

int err;
int retval;
int loopflag = 0;
int loopcount;
int maxloop = -1;
unsigned int addr;
struct sockaddr_in server;
struct hostent *hp;
WSADATA wsaData;
WORD wVersionRequested;

SOCKET conn_sock;

wVersionRequested = MAKEWORD(2,0); // creo la variabile che contiene la versione della wsock32.dll (winsock ver 2.0)

err = WSAStartup(wVersionRequested,&wsaData); // inizializzo la wsock32.dll

if(err != 0) {
printf(" Nessuna winsock trovata!\n");
fprintf(stderr,"WSAStartup failed with error %d\n",WSAGetLastError());
WSACleanup();
return(-1);
} else
printf(" Vers$ %d \n nHVer$ %d \n nMaxSock %d \n nMaxUdpDg %d \n Description: %s\n Status: %s\n\n",
wsaData.wVersion,
wsaData.wHighVersion,
wsaData.iMaxSockets,
wsaData.iMaxUdpDg,
wsaData.szDescription,
wsaData.szSystemStatus);

printf(" --> CLIENT UDP <--\n\n");

/*
Attempt to detect if we should call gethostbyname() or gethostbyaddr()
*/

if(isalpha(server_name[0])) { // server address is a name
hp = gethostbyname(server_name);
} else { // convert nnn.nnn address to a usable one
addr = inet_addr(server_name);
hp = gethostbyaddr((char *)&addr,4,AF_INET);
}

if(hp == NULL) {
fprintf(stderr," Client --> Cannot resolve address [%s] : Error %d\n",
server_name,WSAGetLastError());
WSACleanup();
exit(1);
}

// Copy the resolved information into the sockaddr_in structure

memset(&server,0,sizeof(server));
memcpy((&server.sin_addr),hp->h_addr,hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = htons(port);

conn_sock = socket(AF_INET,SOCK_DGRAM,0);

if(conn_sock< 0) {
fprintf(stderr," Client --> Error opening socket : Error %d\n",WSAGetLastError());
WSACleanup();
return(-1);
}

/*
Notice that nothing in this code is specific to whether we
are using UDP or TCP.
We achieve this by using a simple trick.
When connect() is called on a datagram socket, it does not
actually establish the connection as a stream (TCP) socket
would. Instead, TCP/IP establishes the remote half of the
(LocalIPAddress, LocalPort, RemoteIP, RemotePort) mapping.
This enables us to use send() and recv() on datagram sockets,
instead of recvfrom() and sendto()
*/

printf(" Client connecting to: %s\n",hp->h_name);

// while(ch != 0x1b) {
// ch = getchar();
// getline(cin,s,'\n');
// err = sendto(conn_sock,&ch,sizeof(Buffer),0,(struct sockaddr *)&server,sizeof(struct sockaddr));
// }

err = sendto(conn_sock,Buffer,sizeof(Buffer),0,(struct sockaddr *)&server,sizeof(struct sockaddr));

if(err == SOCKET_ERROR) {
fprintf(stderr," sendto() failed: %d\n",WSAGetLastError());
WSACleanup();
return(-1);
}


// --------------------------------------------------------------------------------

/*

ESEMPIO SOTTO LINUX

if((numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0,
(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
perror("sendto");
exit(1);
}
printf("sent %d bytes to %s\n", numbytes,
inet_ntoa(their_addr.sin_addr));

/*
FUNZIONE SENDTO()

int sendto(SOCKET s,const char FAR * buf,int len,int flags,const struct sockaddr FAR * to,int tolen);

Parameters:
s
[in] A descriptor identifying a (possibly connected) socket.
buf
[in] A buffer containing the data to be transmitted.
len
[in] The length of the data in buf.
flags
[in] An indicator specifying the way in which the call is made.
to
[in] An optional pointer to the address of the target socket.
tolen
[in] The size of the address in to.

*/
/*
ESEMPIO con FTP protocol

char mess[100]; // il messaggio inviato dal Server (max 100 byte);
int n = 0;

n = recv(miosock,mess,100,0); // si riceve il messaggio di benvenuto dal Server

mess[n] = 0; // n rappresenta la lunghezza del messaggio in arrivo (max 100 byte)
printf(mess); // lo visualizzo

while(ch != 0x1b) { // finchè ch è diverso da ESC
ch = getchar(); // aspetto un carattere
send(miosock,&ch,1,0); // e lo invio al Server
}


/*

while(ch!=0x1b) { // finchè ch è diverso dal tasto ESC
ch = getchar(); // aspetto un carattere
send(miosock,&ch,1,0); // e lo invio al Server
}


// */

// ----------------------------------------------------------------------------------------------------------

/*
if(connect(conn_sock,(struct sockaddr*)&server,sizeof(server)) == SOCKET_ERROR) {
fprintf(stderr," connect() failed: %d\n",WSAGetLastError());
WSACleanup();
return(-1);
}
*/

// cook up a string to send

loopcount = 0;

while(1) {

wsprintf(Buffer," This is a small message [number %d]",loopcount++);
retval = send(conn_sock,Buffer,sizeof(Buffer),0);

if(retval == SOCKET_ERROR) {
fprintf(stderr," send() failed: error %d\n",WSAGetLastError());
WSACleanup();
return -1;
}

printf(" Sent Data [%s]\n",Buffer);

retval = recv(conn_sock,Buffer,sizeof(Buffer),0);

if (retval == SOCKET_ERROR) {
fprintf(stderr," recv() failed: error %d\n",WSAGetLastError());
closesocket(conn_sock);
WSACleanup();
return -1;
}

/*
We are not likely to see this with UDP, since there is no
'connection' established.
*/

if (retval == 0) {
printf(" Server closed connection\n");
closesocket(conn_sock);
WSACleanup();
return -1;
}

printf(" Received %d bytes, data [%s] from server\n",retval,Buffer);

if(!loopflag) {
printf(" Terminating connection\n");
break;
}
else {
if ( (loopcount >= maxloop) && (maxloop >0) )
break;
}
}

closesocket(conn_sock);
WSACleanup();
getch();

return(0);

}
Rigo007 è offline   Rispondi citando il messaggio o parte di esso
Old 27-09-2004, 11:30   #3
Rigo007
Member
 
L'Avatar di Rigo007
 
Iscritto dal: Oct 2001
Città: Trento
Messaggi: 164
/*
Simple UDP server using Winsock 2.0
*/

#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define DEFAULT_PORT 5001

int main() {

char Buffer[128];
unsigned short port = DEFAULT_PORT;
int retval;
int fromlen;
int err;
struct sockaddr_in local,from;
WSADATA wsaData; // variabile che contiene i parametri di WSAStartup()
WORD wVersionRequested;

wVersionRequested = MAKEWORD(2,0); // creo la variabile che contiene la versione della wsock32.dll (winsock ver 2.0)

err = WSAStartup(wVersionRequested,&wsaData); // inizializzo la wsock32.dll

printf(" --> SERVER UDP <--\n\n");

if(err != 0) {
printf(" Nessuna winsock trovata!\n");
exit(1);
} else
printf(" Vers$ %d \n nHVer$ %d \n nMaxSock %d \n nMaxUdpDg %d \n Description: %s\n Status: %s\n\n",
wsaData.wVersion,
wsaData.wHighVersion,
wsaData.iMaxSockets,
wsaData.iMaxUdpDg,
wsaData.szDescription,
wsaData.szSystemStatus);

/*
Il codice soprastante serve per inizializzare la wsock32.dll, tramite la funzione WSAStartup()
che è specifica di Windows; le vengono passate come argomenti la versione di libreria richiesta
e l'indirizzo ad una struttura WSAData, che verrà riempita dalla WSAStartup con informazioni
relative alla libreria stessa.
In caso di errore la WSAStartup ritorna un codice diverso da 0 che stà ad indicare l'errore in questione
(fai riferimento al file winsock.h per la lista dei #define).
Il prefisso WSA stà ad indicare Windows Socket API.
*/

SOCKET listen_socket ,msgsock;

local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY;

// Port must be in Network Byte Order

local.sin_port = htons(port);

listen_socket = socket(AF_INET,SOCK_DGRAM,0);

if(listen_socket == INVALID_SOCKET) {
fprintf(stderr," socket() failed with error %d\n",WSAGetLastError()); // scrivo su file
WSACleanup();
return(-1);
}

/*
bind() associates a local address and port combination with the
socket just created. This is most useful when the application is a
server that has a well-known port that clients know about in advance.
*/

err = bind(listen_socket,(struct sockaddr*)&local,sizeof(local));

if(err == SOCKET_ERROR) {
fprintf(stderr," bind() failed with error %d\n",WSAGetLastError());
WSACleanup();
return(-1);
}

printf(" --> 'Listening' on port %d, protocol UDP\n",port);

while(1) {

fromlen = sizeof(from);

msgsock = listen_socket;

/*
for SOCK_DGRAM (UDP), the server will do
recvfrom() and sendto() in a loop.
*/

retval = recvfrom(msgsock,Buffer,sizeof(Buffer),0,(struct sockaddr*)&from,&fromlen);
printf(" Received datagram from %s\n",inet_ntoa(from.sin_addr));

if(retval == SOCKET_ERROR) {
fprintf(stderr," recvfrom() failed with error %d\n",WSAGetLastError());
closesocket(msgsock);
continue;
}
if(retval == 0) {
printf( " Client closed connection\n");
closesocket(msgsock);
continue;
}

printf(" Received %d bytes, data [%s] from client\n",retval,Buffer);

printf(" Echoing same data back to client\n");

retval = sendto(msgsock,Buffer,sizeof(Buffer),0,(struct sockaddr*)&from,fromlen);

if(retval == SOCKET_ERROR) {
fprintf(stderr," sendto() failed with error %d\n",WSAGetLastError());
}

printf(" UDP server looping back for more requests\n");

continue;
}
}
Rigo007 è offline   Rispondi citando il messaggio o parte di esso
 Rispondi


AMD Ryzen 5 7500X3D: la nuova CPU da gaming con 3D V-Cache per la fascia media AMD Ryzen 5 7500X3D: la nuova CPU da gaming con ...
SONY BRAVIA 8 II e BRAVIA Theatre System 6: il cinema a casa in formato compatto SONY BRAVIA 8 II e BRAVIA Theatre System 6: il c...
KTC H27E6 a 300Hz e 1ms: come i rivali ma a metà prezzo KTC H27E6 a 300Hz e 1ms: come i rivali ma a met&...
Cineca inaugura Pitagora, il supercomputer Lenovo per la ricerca sulla fusione nucleare Cineca inaugura Pitagora, il supercomputer Lenov...
Mova Z60 Ultra Roller Complete: pulisce bene grazie anche all'IA Mova Z60 Ultra Roller Complete: pulisce bene gra...
Steam Controller: un ritorno in grande s...
Steam Frame, il visore SteamOS che porta...
Steam Machine, Valve ci riprova con le c...
Super Mario Galaxy - Il Film: disponibil...
In Giappone una donna sposa un personagg...
Le batterie allo stato semi-solido arriv...
Una sola iniezione per curare Alzheimer ...
European Democracy Shield: l'Europa arru...
Peugeot Polygon Concept: la guida del fu...
MSI aggiorna la gamma handheld: arrivano...
Mova 1000 in prova, il robot tagliaerba ...
Addio ai plugin Mi Piace e Commenta: il ...
Google e Qualcomm pronte a lanciare i PC...
Italia, nuove regole sul porno da oggi: ...
WhatsApp introduce nuovi strumenti per l...
Chromium
GPU-Z
OCCT
LibreOffice Portable
Opera One Portable
Opera One 106
CCleaner Portable
CCleaner Standard
Cpu-Z
Driver NVIDIA GeForce 546.65 WHQL
SmartFTP
Trillian
Google Chrome Portable
Google Chrome 120
VirtualBox
Tutti gli articoli Tutte le news Tutti i download

Strumenti

Regole
Non Puoi aprire nuove discussioni
Non Puoi rispondere ai messaggi
Non Puoi allegare file
Non Puoi modificare i tuoi messaggi

Il codice vB è On
Le Faccine sono On
Il codice [IMG] è On
Il codice HTML è Off
Vai al Forum


Tutti gli orari sono GMT +1. Ora sono le: 05:13.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Served by www3v