Discussione: Uffa ..i socket..
View Single Post
Old 12-03-2005, 19:27   #2
Fenomeno85
Senior Member
 
L'Avatar di Fenomeno85
 
Iscritto dal: Jun 2002
Città: Provincia De VaRéSe ~ § ~ Lat.: 45° 51' 7" N Long.: 8° 50' 21" E ~§~ Magica Inter ~ § ~ Detto: A Chi Più Amiamo Meno Dire Sappiamo ~ § ~ ~ § ~ Hobby: Divertimento allo Stato Puro ~ § ~ ~ § ~ You Must Go Out ~ § ~
Messaggi: 8895
server
Codice:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <winsock.h>

#define PORT 12345
#define BL 1024
#define localhost "127.0.0.1"

int main() {
        int sock, client_len, fd;
        char buffer[BL];
        struct sockaddr_in sad, cad;

#ifdef WIN32
       WSADATA wsaData;
       WSAStartup (0x0101,&wsaData);
#endif
       memset ((char*)&sad,0,sizeof(sad));
	   sad.sin_family = AF_INET;
	   sad.sin_addr.s_addr = inet_addr(localhost);
	   sad.sin_port = htons ((u_short)PORT);
       sock = socket(PF_INET, SOCK_STREAM, 0);
       if (sock <0) {
          perror("creazione del socket fallita");
          exit(1);
       }
       if(bind(sock, (struct sockaddr *)&sad, sizeof(sad)) <0) {
          perror("bind socket fallita");
          exit(2);
      }
      
      listen(sock, 15);
      while(1) {
          printf ("\nIn attesa di client\n");
          client_len = sizeof(cad);     
          if( (fd=accept(sock, (struct sockaddr *)&cad, &client_len))<0) {
              perror("accept fallita");
              exit(3);
          }
          printf("Comunicazione in arrivo\n");
          char string[] = "Ciao sono il server di prova.\n";
          send (fd, string, sizeof (string), 0); 
          closesocket (fd);
      }
      closesocket(sock);
}
client
Codice:
#include <winsock.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define PORT 12345
#define BL 1024
#define localhost "127.0.0.1"

int main(int argc, char* argv[]) {
int sock, server_len, count;
char buffer[BL];
struct hostent *host;
struct sockaddr_in server, client;

#ifdef WIN32
WSADATA wsaData;
WSAStartup (0x0101,&wsaData);
#endif

sock = socket(PF_INET, SOCK_STREAM, 0);

if (sock <0) {
perror("creazione del socket fallita");
exit(1);
}

host = gethostbyname(argv[1]);
if(host == NULL) {
perror("host inesistente");
exit(2);
}

server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(localhost);
server.sin_port = htons ((u_short)PORT);

if(connect(sock, (struct sockaddr *)&server, sizeof(server))<0){
perror("Errore connect");
exit(3);
}

printf("Connessione Fatta\n");

while((count=recv(sock, buffer, sizeof(buffer),0))>0) {
printf("%d %s\n", count, buffer);
}

closesocket(sock);
system ("PAUSE");
return (EXIT_SUCCESS);
}
~§~ Sempre E Solo Lei ~§~
__________________
Meglio essere protagonisti della propria tragedia che spettatori della propria vita
Si dovrebbe pensare più a far bene che a stare bene: e così si finirebbe anche a star meglio.
Non preoccuparti solo di essere migliore dei tuoi contemporanei o dei tuoi predecessori.Cerca solo di essere migliore di te stesso
Fenomeno85 è offline   Rispondi citando il messaggio o parte di esso