LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2012, 04:51 AM   #1
crazystudentc
LQ Newbie
 
Registered: Oct 2012
Posts: 2

Rep: Reputation: Disabled
Server/Client Go-Back-N


Hi, I have a little side project I have to do, but for the life of me I cant figure out the information.

Ive got the following two source codes

Server1.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>


#define MYPORT "4951"	

// This is the size of our receiving buffer
#define MAXBUFLEN 100


int main(void)
{
	int       sockfd;
	struct addrinfo hints, *servinfo, *p;
	int       rv;
	int       numbytes;
	struct    sockaddr_storage their_addr;
	char      buf[MAXBUFLEN];
	socklen_t addr_len;

        
	memset(&hints, 0, sizeof hints);
	hints.ai_family   = AF_UNSPEC;  
	hints.ai_socktype = SOCK_DGRAM;
	hints.ai_flags    = AI_PASSIVE; 

        // COMMENT -- What does the following function do?
        // COMMENT -- Why is the first parameter to getaddrinfo NULL?
	if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
		return 1;
	}

	
	for(p = servinfo; p != NULL; p = p->ai_next) 
        {
		if ((sockfd = socket(p->ai_family, p->ai_socktype,
				p->ai_protocol)) == -1) {
			perror("listener: socket");
			continue;
		}

		if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
			close(sockfd);
			perror("listener: bind");
			continue;
		}

		break;
	} // END FOR

	if (p == NULL) {
		fprintf(stderr, "listener: failed to bind socket\n");
		return 2;
	}

	freeaddrinfo(servinfo);

	printf("server: waiting to recvfrom...\n");

 
    
      while(1) 
      {
	
	addr_len = sizeof their_addr;
	if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
		(struct sockaddr *)&their_addr, &addr_len)) == -1) 
        {
		perror("recvfrom");
		exit(1);
	}

        
	buf[numbytes] = '\0';
	printf("server: received... \"%s\"\n", buf);

       
   

       } // END WHILE -- the main loop

      //  -----------------------------------------------------------------------
      // Close the socket to the channel.
      close(sockfd);

      return 0;
}
and client1.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>

// The channel's port
#define CHANNELPORT "4950"
// Our receiving buffer length
#define MAXBUFLEN   100
// Size of our frame
#define MAXMESGLEN  1

int main(int argc, char *argv[])
{
	int sockfd;                          
	struct addrinfo hints, *servinfo, *p; 
	int rv;                               
	int numbytes;                         
        socklen_t addr_len;                   
        struct sockaddr_storage their_addr;   

        char buf[MAXBUFLEN];                  
        char myframe[MAXMESGLEN];             

        char *mesg = "A";                     // Message to be sent

        
        fd_set fds;
        int rc;
        struct timeval timeout;


     
	memset(&hints, 0, sizeof hints);
	hints.ai_family   = AF_UNSPEC;        
	hints.ai_socktype = SOCK_DGRAM;      

        
	if ((rv = getaddrinfo("127.0.0.1", CHANNELPORT, &hints, &servinfo)) != 0) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
		return 1;
	}

	
	for(p = servinfo; p != NULL; p = p->ai_next) 
        {
		if ((sockfd = socket(p->ai_family, p->ai_socktype,
				p->ai_protocol)) == -1) {
			perror("client: socket");
			continue;
		}

		break;
	}

       
	if (p == NULL) {
		fprintf(stderr, "client: failed to bind socket\n");
		return 2;
	}

        myframe[0] = mesg[0];

      
	if ((numbytes = sendto(sockfd, myframe, MAXMESGLEN, 0,
		               p->ai_addr, p->ai_addrlen)) == -1) 
        {
		perror("talker: sendto");
		exit(1);
	}

        printf("Done Sending: %d\n", numbytes);

     
	freeaddrinfo(servinfo);
	close(sockfd);
	return 0;
}
Basically I have this code compiled, all works, Im using IdealChannel and EvilChannel, which are two channels im meant to test things on.


In My first step which im stuck on, is I need to modify the above codes to include Go-Back-N. I have to transfer the letters in the char *mesg = "A", and I also have to edit char* mesg so it send a string of ABCD, but sending one letter at a time, and transfer the letters using go-back-n as well.

I was wondering how exactly am I supposed to do that, and how do I then calculate the average round Trip Time of the channel.

I really want to understand this, as then it will help me in the future with my actaul assignments as i know similar things will be coming up soon.

Regards
 
Old 10-24-2012, 07:55 AM   #2
crazystudentc
LQ Newbie
 
Registered: Oct 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
I have modified client.c to send a large string, but the rest so far i have problems with

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>

// The channel's port -- do not change
#define CHANNELPORT "4950"
// Our receiving buffer length
#define MAXBUFLEN   100
// Size of our frame
#define MAXMESGLEN  1

int main(int argc, char *argv[])
{
	int sockfd;                           
	struct addrinfo hints, *servinfo, *p; 
	int rv;                               
	int numbytes;                         
        socklen_t addr_len;                   
        struct sockaddr_storage their_addr;   

        char buf[MAXBUFLEN];                  
        char myframe[MAXMESGLEN];             

        char *mesg = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123";                     
	int i;

        // used for select
        fd_set fds;
        int rc;
        struct timeval timeout;


	memset(&hints, 0, sizeof hints);
	hints.ai_family   = AF_UNSPEC;        // COMMENT
	hints.ai_socktype = SOCK_DGRAM;       // COMMENT

	if ((rv = getaddrinfo("127.0.0.1", CHANNELPORT, &hints, &servinfo)) != 0) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
		return 1;
	}

	// COMMENT
	for(p = servinfo; p != NULL; p = p->ai_next) 
        {
		if ((sockfd = socket(p->ai_family, p->ai_socktype,
				p->ai_protocol)) == -1) {
			perror("client: socket");
			continue;
		}

		break;
	}

        // If we can't get a socket ...
	if (p == NULL) {
		fprintf(stderr, "client: failed to bind socket\n");
		return 2;
	}

     
	for(i=0;i<30;i++)        
	{
		myframe[0] = mesg[i];

       		// COMMENT
		if ((numbytes = sendto(sockfd, myframe, MAXMESGLEN, 0,
		               p->ai_addr, p->ai_addrlen)) == -1) 
        	{
			perror("talker: sendto");
			exit(1);
		}	

        	printf("Done Sending: %d\n", numbytes);
	}
        
      
	freeaddrinfo(servinfo);
	close(sockfd);
	return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
zmq client server program ..my client is working but server is not..pls help batman4 Programming 2 08-13-2012 07:44 AM
how to make the server to get its log data back to it after connecting to client prakash.akumalla Linux - Newbie 1 06-25-2008 05:54 PM
DHCP Suse 9.2 Server wont route pings back to client mikfig Linux - Networking 2 01-12-2005 03:13 PM
back up evolution mail client setting melinda_sayang Linux - General 2 06-30-2004 03:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration