Click here to Skip to main content
15,041,788 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
K so i have a client and a server and i am trying to send from client 2 vectors of numbers and the server will receive them and send back another vector with numbers from vector 1 and not in vector 2 received from client anyway this is bla bla .. i did the simple sento() from client and recvfrom() in server, but trampled at " HOW TO send from server back to the client and receive from client ?
the comments and this writed are in romanian ignore that:)

   /*Client*/


#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>

#define max 100	

int main() {				/* CLIENT UDP CLIENT LAB_2 CLIENT*/
	int sir1[max] , sir2[max];	/*2 siruri<vector> de numere */
	char s[max];			/*citim un string s de la tastatura care contine numerele*/
	char * ps;			/*pointer la string s pt strtok (tokens)*/
	int c;				/*c descriptor de socket client*/
	int cod, lsi, ls1 ,ls2;		/*cod return  ls1 lungime sir 1 , ls2 lungime sir 2*/
	struct sockaddr_in server, client ;
/***************************************************************************************************/
	c = socket(AF_INET , SOCK_DGRAM , 0 );			/*cream socketul cu  descriptorul c*/
	if ( c < 0 ) {
		fprintf( stderr , "Eroare la creare socket in client .\n" );
		return -1;
	}

	memset(&client , 0 , sizeof(struct sockaddr_in) );		/*initializam */
	client.sin_family = AF_INET;
	client.sin_port = htons(6363);
	client.sin_addr.s_addr = inet_addr("127.0.0.1");
/*****************************************************************************************************/
/*        cod = bind ( c , (struct sockaddr *) &client , sizeof(struct sockaddr) );*/	/*facem bind */
/*        if ( cod < 0 ) {
                fprintf( stderr , "Eroare la bind in client. \n");
                return -1;
        }*/
/********************************************************************************************************************************/
	printf("Dati primul sir de numere : \n");		/*citim primul sir ca si string si il convertim dupaia in numere*/
	fgets(s,99,stdin);
	int i=-1;
	ps = strtok (s," .-,\n\0");		/*intre numere poate fi orice . , - + excludem enter si \0 nullul de sfarsit*/
	while( ps !=  NULL ) {
		//printf(" %s \n", ps);
		i++;
		sir1[i] = atoi(ps);
		ps = strtok (NULL ," .-,\n\0");
	}//while
	ls1=i;
	int j;
	printf("Sirul 1 de numere: ");
	for(j=0; j<= ls1; j++)
		printf("%d ,",sir1[j]);
	printf("\nDe lungime %d \n",ls1+1);
/******************************************************************************************************/	
	printf("\n\nDati al doilea sir de numere : \n");	/*citim al doilea sir de numere */
	fgets(s,99,stdin);
	//printf(" %s \n",s);
	i=-1;
	ps = strtok(s , " .-,\n\0");		
	while(ps != NULL) {
		i++;
		sir2[i] = atoi(ps);
		ps = strtok(NULL , " .-,\n\0"); 	
	}//while
	ls2 = i;
	printf("Sirul 2 de numere : ");
	for(j=0; j<= ls2; j++)
		printf("%d ,",sir2[j]);
	printf("\nDe lungime %d \n",ls2+1);
/******************************************************************************************************/
	/*trimitem prima data lungimea dupa care primul sir de numere*/
	sendto(c , &ls1 , sizeof(int) , 0 , (struct sockaddr * ) &client ,sizeof(client) );
	 for(i=0 ; i <= ls1 ; i++)
		sendto(c , &sir1[i] , sizeof(int) , 0 , (struct sockaddr * ) &client , sizeof(client) );
	/*trimitem lungimea dupa care al doilea sir de numere*/
	sendto(c , &ls2 , sizeof(int) , 0 , (struct sockaddr * ) &client , sizeof(client) );
	 for(i=0 ; i <= ls2 ; i++)
		sendto(c , &sir2[i] , sizeof(int) , 0 , (struct sockaddr * ) &client , sizeof(client) );
	
   /*  THE PROBLEM i get some warning here and i am not sure wthat to put as parameters for recvfrom */

	recvfrom(c , &lsi , sizeof(int) , 0 , (struct sockaddr *) &client , sizeof(client) );
	printf("\nI GET FROM SERVER  %d " , lsi );

printf("\n");
close(c);
return 0;
}//end main


                     /*SERVER*/

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h> /*PENTRU MEMSET*/
#define max 100
int main() {             /* SERVER  UDP  SERVER  LAB_2 SERVER */
    int si[max],sir1[max], sir2[max];
    int s;
    int cod , i,lsi, ls1, ls2 ;
    struct sockaddr_in server , client;
    int clength=sizeof(struct sockaddr_in);
    s = socket( AF_INET , SOCK_DGRAM , 0 );
    if ( s < 0 ) {
        fprintf( stderr , "Eroare la creare socket in server . \n" );
        return -1;
    }
    memset( &server , 0 , sizeof(struct sockaddr_in) );
    server.sin_family = AF_INET;
    server.sin_port = htons(6363);
    server.sin_addr.s_addr = INADDR_ANY;
    cod = bind ( s , (struct sockaddr *) &server , sizeof(struct sockaddr) );
    if ( cod < 0 ) {
        fprintf( stderr , "Eroare la bind in server. \n");
        return -1;
    }
    while(1) {
        printf("\nserver astept\n");
/***************primim primul sir de numere**********************************************/
        recvfrom(s , &ls1 ,sizeof(int),0 ,(struct sockaddr *) &client , &clength );
            printf("Am primit lungimea : %d \n" , ls1+1 );
        for(i=0; i<=ls1; i++)
            recvfrom(s , &sir1[i], sizeof(int), 0 , (struct sockaddr * ) &client , &clength );
        printf("AM primit de la client sirul 1 : ");
            for(i=0 ; i <= ls1 ; i++ )
                printf(" %d",sir1[i]);
/***************primim al doilea sir de numere ******************************************/
        recvfrom(s , &ls2 ,sizeof(int),0 ,(struct sockaddr *) &client , &clength );
                    printf("\nAm primit lungimea : %d \n" , ls2+1 );
                for(i=0; i<=ls2; i++)
                        recvfrom(s , &sir1[i], sizeof(int), 0 , (struct sockaddr * ) &client , &clength );
                printf("AM primit de la client sirul 2 : ");
                    for(i=0 ; i <= ls2 ; i++ )
                        printf(" %d",sir1[i]);
    }//end while
    lsi =69;
   /*HOW TO SEND TO CLIENT ?*/ 
sendto( s , &lsi , sizeof(int) , (struct sockaddr *) &client , &clength );

close(s);
return 0;
}//end main
Posted
Comments
xtygerx 16-May-11 7:21am
   
LOOK just at the end for what i need to be answered , tank you ! ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900