Пример #1
0
/*receive two messages via s new socket,
	print out the messages received and close the socket
	bind to  any of the addresses of this computer
	using port given as argument */
void receiver(int port)
{
	char message1[SIZE], message2[SIZE];
	struct sockaddr_in mySocketAddress, aSocketAddress;
	int s,aLength, n;
	int i;

	if((s = socket(AF_INET, SOCK_DGRAM, 0))<0) {
		perror("socket failed");
		return;
	}
	makeReceiverSA(&mySocketAddress, port);

	if( bind(s, (struct sockaddr *)&mySocketAddress, sizeof(struct sockaddr_in))!= 0){
		perror("Bind failed\n");
		close(s);
		return;
	}

	printSA(mySocketAddress);
	aLength = sizeof(aSocketAddress);
	aSocketAddress.sin_family = AF_INET;
	if((n = recvfrom(s, message1,  SIZE, 0, (struct sockaddr *)&aSocketAddress, &aLength))<0)
		perror("Receive 1") ;
	else{
		printSA(aSocketAddress);
		for(i=0; i<n; i++) printf("M:%d", message1[i]); 
		printf("\n Received Message:(%s)length = %d \n",
			message1,n);
	}
	close(s);
}
Пример #2
0
void P() {
	int i;
	ENTRY e, e1, e2;

	initEntry(sa1, 0, 1, -5);
	initEntry(sa1, 0, 3, 8);
	initEntry(sa1, 2, 0, 20);
	initEntry(sa1, 3, 3, -3);
	printf("Sparse array 1:\n");
    printSA(sa1);

	initEntry(sa2, 0, 2, -2);
	initEntry(sa2, 0, 3, 5);
	initEntry(sa2, 2, 0, -15);
	printf("Sparse array 2:\n");
    printSA(sa2);

	addSA(sa1, sa2, sa3);
	printf("Sparse array 3:\n");
    printSA(sa3);
}
Пример #3
0
void sender(char *message1, char *message2,char *machine, int port)
{
	int s, n;
	char message[SIZE];
	struct sockaddr_in mySocketAddress, yourSocketAddress;

	if(( s = socket(AF_INET, SOCK_DGRAM, 0))<0) {
		perror("socket failed");
		return;
	}
/*
	 if((x = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &arg, sizeof(arg))<0)
		perror("setsockopt SO_BROADCAST---");
		exit(-1);
  */
	makeLocalSA(&mySocketAddress);
	if( bind(s, (struct sockaddr *)&mySocketAddress, sizeof(struct sockaddr_in))!= 0){
		perror("Bind failed\n");
		close (s);
		return;
	}
	printSA(mySocketAddress);
	makeDestSA(&yourSocketAddress,machine, port);
	printSA(yourSocketAddress);
	strcpy(message,message1);
	if( (n = sendto(s, message, strlen(message), 0, (struct sockaddr *)&yourSocketAddress,
		sizeof(struct sockaddr_in))) < 0)
		perror("Send failed\n");
	if(n != strlen(message)) printf("sent %d\n",n);
	strcpy(message,message2);
	if( (n = sendto(s, message, strlen(message), 0, (struct sockaddr *)&yourSocketAddress,
		sizeof(struct sockaddr_in))) < 0)
		perror("Send failed\n");
	if(n != strlen(message)) printf("sent %d\n",n);
	close(s);
}