Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    146

    [C] problema socket

    Salve, ho questi 2 codici un client e un server dove il client dovrebbe mandare il file prova.txt al server, ma nel momento in cui il server resta in ascolto e il client invia il file scompare il testo del file e non viene inviato nulla... Perchè?? Grazie

    CLIENT
    codice:
    #include <sys/socket.h>
    
    #include <sys/types.h>
    
    #include <netinet/in.h>  
    
    #include <arpa/inet.h>
    
    #include <stdio.h>
    
     
    
    #define PORT 2080
    
     
    
    main()
    
    {
    
    	int sock;
    
    	sock =  socket(AF_INET,SOCK_STREAM,0);
    
    	struct sockaddr_in serv;
    
     
    
    	serv.sin_port = htons(PORT);
    
    	printf("%x %x\n",PORT,htons(PORT));
    
    	serv.sin_family = AF_INET;
    
    	serv.sin_addr.s_addr = inet_addr("127.0.0.1");
    
    	printf("client connecting\n");
    
    	connect(sock, (struct sockaddr *)&serv,sizeof(serv));
    
    	char buf[50];
    
    	FILE* fp = fopen("prova.txt","w");
    
    	while(1){
    
    		//bzero(buf,sizeof(buf));
    
    		read(sock,buf,50);
    
    		if(strcmp(buf,"quit1234")==0)
    
    		{
    
    			break;	
    
    		}
    
    		fprintf(fp,"%s",buf);
    
    	}
    
    	fclose(fp);
    
    }
    SERVER
    codice:
    #include <sys/socket.h>
    
    #include <sys/types.h>
    
    #include <netinet/in.h>  
    
    #include <arpa/inet.h>
    
    #include <stdio.h>
    
     
    
    #define PORT 2080
    
     
    
    main()
    
    {
    
    	int sock1, sock2, clength;
    
    	sock1 =  socket(AF_INET,SOCK_STREAM,0);
    
    	struct sockaddr_in serv, cli;
    
     
    
    	serv.sin_family = AF_INET;
    
    	serv.sin_port = htons(PORT);
    
    	serv.sin_addr.s_addr = inet_addr("127.0.0.1");
    
    	bind(sock1, (struct sockaddr *)&serv, sizeof(serv));
    
    	listen(sock1, 5);
    
    	clength = sizeof(cli);
    
    	int i = 0;
    
    	char buf[50];
    
    	sock2 = accept(sock1, (struct sockaddr *)&cli, &clength);
    
    	printf("\n Client Connected\n");
    
    	FILE* fp = fopen("prova.txt", "r");
    
    	while(!feof(fp)){
    
    		//bzero(buf,sizeof(buf));
    
    		fread(buf, sizeof(char), 50, fp);
    
    		write(sock2, buf, 50);
    
    	}
    
    	write(sock2, "quit1234", 50);
    
    	fclose(fp);
    
    	return 0;
    
    }

  2. #2
    Utente di HTML.it L'avatar di XWolverineX
    Registrato dal
    Aug 2005
    residenza
    Prague
    Messaggi
    2,565
    Nel client manca il bind e i listen del socket.
    Ma sei sicuro che si possa usare read e write al posto di send e recv?
    "Se proprio devono piratare, almeno piratino il nostro." (Bill Gates)

    "Non è possibile che 2 istituzioni statali mi mettano esami nello stesso giorno." (XWolverineX)

    http://xvincentx.netsons.org/programBlog

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.