0

I am working on a project where I have to do a program Client/Server where I have to test the connection , send/receive files and list all the files saved on the server.

I am on the first step , and I am asked to enter manually the port number and the IP address and I am blocked on the IP address declaration..

There is my code

void connection(){  
    int port;
    int sockfd,connfd;
    struct sockaddr_in servaddr,cli;
    sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
    printf("socket creation failed...\n");
    exit(0);
}
else
    printf("Socket successfully created..\n");
    printf("Enter Port Number \n");
    scanf("%d",&port);
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
    servaddr.sin_port=htons(port);
if(connect(sockfd,(SA *)&servaddr,sizeof(servaddr))!=0)
{
    printf("connection with the server failed...\n");
    printf("Program Closed\n");
    exit(0);
}
else
    printf("connected to the server..\n");
    printf("listening from Server\n");
    func(sockfd);
    close(sockfd);}

Now If I want to declare a variable and put my IP "127.0.0.1" in it and use it in this line servaddr.sin_addr.s_addr=inet_addr ?

And after establishing the connection to the server, I am able to send and receieve messages , but I want to show the traces (Frames) with their size in bytes etc. Any idea?

Thank you

Zok
  • 355
  • 2
  • 15
  • What do you mean "frames"? Ethernet frames? TCP packets? What about variable for IP? Do you want to know how to enter that IP from console? – Orest Hera Sep 30 '15 at 21:49
  • Well when I send a message from Client to server it prints on the console Sending the message 127.0.0.1:58482 : Transmission of frame 0 (31 bytes) 127.0.0.1:58482 : Activation of timeout 1000ms 127.0.0.1:58482 : Received. And on the Server side it shows 127.0.0.1:58482 : Received the transmission of frame 0 (4 bytes) 127.0.0.1:58482 : Acquittement of transmission frame 0 127.0.0.1:58482 : Accepted Received of command:TEST. I think we can show these info by using the send() and receive functions? And yeah I scan the Port using and integer by scanf , then I want to use another variable for IP – Zok Sep 30 '15 at 22:01

1 Answers1

0

It is simpler part with variable for IP. You can use char array with fixed size 16 to avoid dynamic memory allocation, since 16 bytes is enough to store string with valid IPv4 address (for example "255.255.255.255" - 3 dots + 12 digits + 1 null symbol terminating string).

The code for reading IP:

char ip[16];
scanf("%15s", ip);
/* if needed it is possible to write some function for validation of
 * enetered string to check that it is valid IP address string: 4 decimal
 * numbers in range 0...255 separated by dots */
/* ... */
servaddr.sin_addr.s_addr = inet_addr(ip);

The string "%15s" in scanf means that we want to read a null terminated string s and its lenght should be maximum 15 characters (+1 for null is not counted in that number).

Regarding frames at first I though that you want to capture separate packets. However, now it looks that you want just reguaral client-server communation using TCP sockets with some prints about activities.

Let's consider client as an example. You already have connected state. The next you should prepare data buffer to send. Before sending you can print "Transmission of frame...".

Then you should prepare for timeout. That is here How to set socket timeout in C when making multiple connections? (the answer https://stackoverflow.com/a/4182564/4023446). You can print "Activation of timeout 1000ms".

Data sending send(). This function returns number of sent bytes. So, if that number is equal with number of data that you was sending it is possible to print "Sent".

Similarly it is possible to describe read operations on server by phrases according to stages: connected, reading (also possible with timeout), received: "TEST".

Community
  • 1
  • 1
Orest Hera
  • 6,706
  • 2
  • 21
  • 35
  • Thank you so much for the informations you provided , you really helped me. the IP works now and tomorrow I will read the posts and try to do the same :) and if I face any problems I will post here – Zok Oct 01 '15 at 00:59