Thread: tcp simulation in c to be changed to fit omnet++ 4.0 software using c++ & java

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Question tcp simulation in c to be changed to fit omnet++ 4.0 software using c++ & java

    hello,

    This is a client and server program on c from some example code.I need help to change it to a
    code which works on omnet++ 4.0 version software, which requires java and c++. i would be highly obliged if anyone could help me out with this as its very urgent for a project.
    Many thanks for any suggestions,

    Robin


    Code:
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    
    
    ///   CLIENT   
    
    
    int main(int argc, char *argv[])
    {
    	printf("This is the client program\n");
    
    	int sockfd;
    	int len, rc ;
    	struct sockaddr_in address;
    	int result;
    	char ch = 'A';
    
       //Create socket for client.
    	sockfd = socket(PF_INET, SOCK_STREAM, 0);
    	if (sockfd == -1) { 
    		perror("Socket create failed.\n") ; 
    		return -1 ; 
    	} 
    	
    	//Name the socket as agreed with server.
    	address.sin_family = AF_INET;
    	address.sin_addr.s_addr = inet_addr("127.0.0.1");
    	address.sin_port = htons(7734);
    	len = sizeof(address);
    
    	result = connect(sockfd, (struct sockaddr *)&address, len);
    	if(result == -1)
    	{
    		perror("Error has occurred");
    		exit(-1);
    	}
    
    	while ( ch < 'Y') {
    
    		//Read and write via sockfd
    		rc = write(sockfd, &ch, 1);
    		printf("write rc = %d\n", rc ) ; 
    		if (rc == -1) break ; 
    		
    		read(sockfd, &ch, 1);
    		printf("Char from server = %c\n", ch);
    		//if (ch == 'A') sleep(5) ;  // pause 5 seconds 
    	} 
    	close(sockfd);
    
    	exit(0);
    }
    SERVER 
    
    Code:
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    
    ///   SERVER   
    
    int main(int argc, char *argv[])
    {
    	//Declaring process variables.
    	int server_sockfd, client_sockfd;
    	int server_len ; 
    	int rc ; 
    	unsigned client_len;
    	struct sockaddr_in server_address;
    	struct sockaddr_in client_address;
    
    	//Remove any old socket and create an unnamed socket for the server.
    	server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    	server_address.sin_family = AF_INET;
    	server_address.sin_addr.s_addr = htons(INADDR_ANY);
    	server_address.sin_port = htons(7734) ; 
    	server_len = sizeof(server_address);
    
    	rc = bind(server_sockfd, (struct sockaddr *) &server_address, server_len);
    	printf("RC from bind = %d\n", rc ) ; 
    	
    	//Create a connection queue and wait for clients
    	rc = listen(server_sockfd, 5);
    	printf("RC from listen = %d\n", rc ) ; 
    
    	client_len = sizeof(client_address);
    	client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_address, &client_len);
    	printf("after accept()... client_sockfd = %d\n", client_sockfd) ; 
    
    	while(1)
    	{
    		char ch;
    		printf("server waiting\n");
    
    		//Accept a connection
    		//client_len = sizeof(client_address);
    		//client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_address, &client_len);
    		//printf("after accept()... client_sockfd = %d\n", client_sockfd) ; 
    		//Read write to client on client_sockfd
    
    		rc = read(client_sockfd, &ch, 1);
    		printf("RC from read = %d\n", rc ) ; 		
    		if (ch=='X') break ; 
    		ch++;
    		write(client_sockfd, &ch, 1);
    	}
    
    	printf("server exiting\n");
    
    	//close(client_sockfd);
    	close(client_sockfd);
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You mention 3 programming languages, c, c++ and java, neither of which is C# so why you put it here is beyond me.

    Second your question is very vague, what exactly are you having trouble with? A good start would probably be to look around the documentation for the omnet++ library and look at various examples and so on.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    In other words, it's a case of

    "I found this code on the net, plz translate for me because I'm too busy/lazy - kthxbye"
    How To Ask Questions The Smart Way
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Role of c++ in simulation software
    By CChakra in forum C++ Programming
    Replies: 9
    Last Post: 11-18-2008, 02:36 AM
  2. Why C Matters
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 136
    Last Post: 01-16-2008, 09:09 AM
  3. How Cool is Java
    By dukemarlon in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 11-28-2002, 05:24 PM

Tags for this Thread