-
Notifications
You must be signed in to change notification settings - Fork 0
/
mySocket.c
50 lines (34 loc) · 1.21 KB
/
mySocket.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Gregorio Maria Vallé 5^C Informatica 22/01/2024
#include <windows.h>
#include <stdio.h>
#include "mySocket.h"
#define MAXLENIP 16
#define MAXBUFF 128
#define DEFAULTPORT 6000
WSADATA wsaData;
//start WSA
void startWSA(){
WORD wVersionRequested = MAKEWORD(2, 2);
if(WSAStartup(wVersionRequested, &wsaData)) exit(1);
if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2){
WSACleanup();
exit(2);
}
}
//invia la stringa che passiamo come parametro all'indirizzo che passiamo come parametro
void trasmissione(char str[], char ipDest[]) {
SOCKET sockTx;
SOCKADDR_IN addrRemoto;
char sendBuff[MAXBUFF];
char szIPDest[MAXLENIP];
strncpy(szIPDest, ipDest, MAXLENIP);
sockTx = socket(AF_INET, SOCK_DGRAM, 0);
addrRemoto.sin_family = AF_INET;
addrRemoto.sin_port = htons(DEFAULTPORT);
addrRemoto.sin_addr.S_un.S_addr = inet_addr(szIPDest);
strncpy(sendBuff, str, MAXBUFF);
sendto(sockTx, sendBuff, strlen(sendBuff) + 1, 0, (SOCKADDR *) &addrRemoto, sizeof(SOCKADDR));
fflush(stdout);
//printf("Il messaggio: '%s', e' stato inviato a: '%s'\n", str, ipDest);
closesocket(sockTx);
}