Client receives the struct and prints 'c'. If it is 'bye' it closes connection, else it returns to asking the input of 'a' and 'b' and the cycle begins again.
Client:
Expand|Select|Wrap|Line Numbers
- #if defined WIN32
- #include <Winsock2.h>
- #else
- #define closesocket close
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define BUFSIZE 30
- void ClearWinSock() {
- #if defined WIN32
- WSACleanup();
- #endif
- }
- //-----------------------------INIZIALIZZAZIONE WSADATA
- int main (void) {
- #if defined WIN32
- WSADATA wsaData;
- int iResult = WSAStartup (MAKEWORD (2,2), &wsaData);
- if (iResult !=0) {
- printf ("error at WSASturtup\n");
- return 0;
- }
- #endif
- //--------------------------------CREAZIONE SOCKET
- int Csocket;
- Csocket = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (Csocket < 0) {
- printf ("socket creation failed");
- closesocket (Csocket);
- ClearWinSock();
- return 0;
- }
- char s_address[BUFSIZE];
- printf ("Inserisci l'indirizzo dell'host a cui connetterti\n");
- scanf ("%s", s_address);
- int port;
- printf ("Inserisci la porta a cui connetterti\n");
- scanf("%d", &port);
- struct sockaddr_in sad;
- memset (&sad, 0, sizeof(sad));
- sad.sin_family = AF_INET;
- sad.sin_addr.s_addr = inet_addr(s_address);
- sad.sin_port = htons (port);
- //------------------------------CONNESSIONE AL SERVER
- if (connect(Csocket, (struct sockaddr*) &sad, sizeof(sad)) < 0) {
- printf ("failed to connect\n");
- closesocket (Csocket);
- ClearWinSock();
- return 0;
- }
- //-----------------------------RICEZIONE STRINGA DAL SERVER
- char buf[BUFSIZE];
- int read = recv (Csocket, buf, BUFSIZE - 1, 0);
- if (read <=0) {
- printf ("Qualcosa non và!\n");
- }
- else {
- buf[read] = '\0'; //tappo stringa per sicurezza
- printf("Server scrive: %s\n", buf);
- }
- //------------------------------STRUTTURA DI STRINGHE
- struct stringab {
- char a[30];
- char b[30];
- char c[70];
- } ab;
- void* abptr = &ab;
- printf ("Inserisci prima stringa\n");
- scanf ("%s", ab.a);
- printf ("Inserisci seconda stringa\n");
- scanf ("%s", ab.b);
- if (send(Csocket, (char*)abptr, sizeof(ab), 0) != sizeof(ab)) {
- printf("client-send() sent a different number of bytes than expected");
- closesocket(Csocket);
- ClearWinSock();
- system ("pause");
- return 0;
- }
- printf ("\n");
- closesocket (Csocket);
- system ("pause");
- ClearWinSock();
- return 0;
- }
Expand|Select|Wrap|Line Numbers
- #if defined WIN32
- #include <Winsock2.h>
- #else
- #define closesocket close
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define BUFSIZE 30
- void ClearWinSock() {
- #if defined WIN32
- WSACleanup();
- #endif
- }
- int main(void) {
- //---------------------------INIZIALIZZAZIONE WSADATA
- #if defined WIN32
- WSADATA wsaData;
- int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
- if (iResult != 0) {
- printf ("Error at WSAStartup");
- return 0;
- }
- #endif
- //-------------------------------CREAZIONE SOCKET
- int Mysocket;
- Mysocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- //Valutare la presenza di errori per assicurarsi che la socket sia valida.
- if (Mysocket < 0) {
- printf("socket creation failed\n");
- return 0;
- }
- //Assegnazione indirizzo alla socket.
- struct sockaddr_in sad;
- memset(&sad, 0, sizeof(sad));
- sad.sin_family = AF_INET;
- sad.sin_addr.s_addr = inet_addr ("127.0.0.1");
- sad.sin_port = htons (9888);
- //------------------------ASSEGNAZIONE PORTA E IP ALLA SOCKET
- if (bind(Mysocket, (struct sockaddr*) &sad, sizeof(sad)) < 0) {
- printf ("bind() failed\n");
- closesocket(Mysocket);
- return 0;
- }
- //---------------------------SETTAGGIO SOCKET ALL'ASCOLTO
- int qlen = 10; //numero client che il server può gestire
- if (listen (Mysocket, qlen) < 0) {
- printf("listen() failed\n");
- closesocket(Mysocket);
- return 0;
- }
- struct sockaddr_in cad; // struttura per l'indirizzo del cient
- int Csocket; // descrittore socket per il client
- int clientlen; // lunghezza indirizzo del client
- //------------------------------ACCETTA LA CONNESSIONE
- while (1) {
- printf("In attesa di un client con cui comunicare...\n");
- memset(&cad, 0, sizeof(cad));
- clientlen = sizeof(cad); // assegna la dimensione dell'indirizzo del client
- if((Csocket = accept(Mysocket, (struct sockaddr*) &cad, &clientlen)) < 0) {
- printf ("accept failed\n");
- closesocket(Mysocket);
- ClearWinSock();
- return 0;
- }
- printf("Connessione stabilita con il client il cui indirizzo e' %s \n\n", inet_ntoa(cad.sin_addr));
- //---------------------------------------INVIO STRINGA AL CLIENT
- char* inputString = "Connessione avvenuta!";
- int stringlen = strlen(inputString);
- if (send(Csocket, inputString, stringlen, 0) != stringlen) {
- printf("client-send() sent a different number of bytes than expected");
- closesocket(Csocket);
- ClearWinSock();
- system ("pause");
- return 0;
- }
- printf ("\n");
- closesocket (Csocket);
- system ("pause");
- ClearWinSock();
- return 0;
- }