• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

testing

hi all,
My program is to setup connection from a client to 2 servers. However, I don't know setup those 2 servers to test my code.  I can see a weird thing. In [server1] of  config file,  if i change servername = ubuntu1 which i don't have ubuntu1 system, it can detect

 section:[server1] entry1: ubuntu1   entry2:50000
ERROR, no such host


However, it fail can't detect same error when in the second section with servername = ubuntu2. I don't have ubuntu2 system .

Anyway, please advise on how to test my code. tks a lot.

below is the config file
[server1]
servername = ubuntu
port = 50000
[server2]
servername = ubuntu2
port = 37777

client.c
client.h
server.c
0
BeginToLearn
Asked:
BeginToLearn
  • 30
  • 14
  • 9
  • +1
5 Solutions
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Begin,

If you're going to run both of these servers on the same system, they are going to have to listen on different ports.  (Maybe 50000 and 50001.)

Then if you want to connect to these servers by name, edit the file /etc/hosts and add these lines:

ubuntu1 127.0.0.1
ubuntu2 127.0.0.1

That will assign "localhost" (the local machine's IP address) to both of those names.


You can connect to the name, on the correct port(s).  



Kent
0
 
BeginToLearnAuthor Commented:
could you give me some keywords so I can search more if I want to test  on same system or diffeent system once I get home?tks a lot.
0
 
ssnkumarCommented:
I compiled and executed your code.
It works fine.
I think, the only problem is in the config file.
You must be able to access the servername that you specify in the config file.
Try "ping servername" and see if it works.
And this is the server on which your server code should be running.
In my laptop (running CentOs), my /etc/hosts has the following contents:
127.0.0.1               localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6
172.17.6.226    Narendra-Centos.proxyarya.com Narendra-Centos

All the 3 entries correspond to my laptop.
I ran both the server and the client on the same laptop.
I modified the config file as below:
[server1]
servername = localhost
port = 50000
[server2]
servername = localhost
port = 37777

So, I used "localhost" as my servername.

Here below is the o/p that I got when I executed the client:
# ./cl ./
currentdir is ./
section:[server1] entry1: localhost   entry2:50000
message1-> total number of files is 6
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
BeginToLearnAuthor Commented:
I just wonder how you can test it beause the dst will be overlap in the same system. I gonna change the server.c  to accept dest from command  line.

 Now i get the idea on test same system.
How about testing on different systems which are connected by wireless? How to let client look for the right servers to send files?? using IP???

How to make my code can work regardingless to same system or different system? tks a lot.
0
 
ssnkumarCommented:
> I just wonder how you can test it beause the dst will be overlap in the same system
When running 2 instances of the same server on the same system,
although the server ip is same, the server port for both the servers are different.
So, as long as you have different ip for each server, it will run.

> How to let client look for the right servers to send files?? using IP???
Yes, you can use IP.
But, either the server IP has to be a public IP (which is ping'able from client)
or they should be in the same LAN (where both of them share the same subnet).
If you want to use hostname to access the server or client, then you need to register the hostname and corresponding ip address on the DNS server.

> How to make my code can work regardingless to same system or different system?
It is already working, as I explained before.
0
 
BeginToLearnAuthor Commented:
If you want to use hostname to access the server or client, then you need to register the hostname and corresponding ip address on the DNS server. this is the idea i didn't know and help me understand 1 question in my mind. .

I mean in the same system, 2 servers all  will write files to same dst directory. It may corrupt. hihi

0
 
ssnkumarCommented:
> this is the idea i didn't know and help me understand 1 question in my mind
So, you will need to configure and run a DNS in your network.
This will take some effort.
But, you can use /etc/hosts file of Linux machine to get the same behavior.
In client machine, add the ip and hostname of the servers.
Both /etc/hosts and DNS will achieve the same thing - resolve a hostname to an IP address.

> I mean in the same system, 2 servers all  will write files to same dst directory. It may corrupt. hihi
That is true.
So, it is better, if your server takes destination directory as command line. Or make it part of config file.
0
 
BeginToLearnAuthor Commented:
. I am reworking on my server now. At this moment I will only test for in local system for now because i need to work on more features  such as backup, restore, zip, encryption.
0
 
sarabandeCommented:
the server not only should take destination from commandline but port number as well. in the moment two instances of server would use port 50000 what couldn't work on same host.

Sara
0
 
BeginToLearnAuthor Commented:
let me change it too
0
 
BeginToLearnAuthor Commented:
after i change the code on several things to test one client to one server:
   + split code to server.c, server.h --> ok
   + receive argument from command line --> ok
   + manipulate path directory. for example the source path is  /home/ubuntu/program/src , it should be /home/ubuntu/program/dest , which come from command line

Somehow , it is screwed up now

message2-> dirname is : /home/ubuntu/program/src/dir1
message2-> dirname is : /home/ubuntu/program/src/dir1/dir2a
message2-> dirname is : /home/ubuntu/program/src/dir1/dir2a/dir4
message2-> dirname is : /home/ubuntu/program/src/dir1/dir2
message2-> dirname is : /home/ubuntu/program/src/dir1/dir2/dir3
message1-> total number of files is 20
message2 -> filename is :/home/ubuntu/program/dest/dir1/dir2a/dir4/dir2/dir3/test2aa.txt 3947
message2 -> filename is :/home/ubuntu/program/dest/dir1/dir2a/dir4/dir2/dir3/td>
    <td align="right">
     <span style="font-size: 9pt">
     <a href="text_compare_alignment.html"
        onmouseover="document.images.prev.src='prev-hot.png'"
        onmouseout="document.images.prev.src='prev.png'"
        ><img name=prev src="prev.png" bord
failed to open to append
: File name too long

Could you please take a look?
server.c
server.h
client.c
client.h
0
 
sarabandeCommented:
in createDir you change the dirname you got from commandline. you need to use a tmp as you did it in pareseFileName.

in receiveOneDir you have a local string variable 'dirname' what may give very strange errors when having a member variable of same name.

Sara
0
 
BeginToLearnAuthor Commented:
let me fix them . what time do you often leave office?
0
 
sarabandeCommented:
now. but i can go into the net after supper.

Sara
0
 
BeginToLearnAuthor Commented:
I see.

It still has same errors

ubuntu@ubuntu:~/program$ g++ -o server server.c
ubuntu@ubuntu:~/program$ ./server /home/ubuntu/program/dest 50000
message2-> dirname is : /home/ubuntu/program/src/dir1
message2-> dirname is : /home/ubuntu/program/src/dir1/dir2a
message2-> dirname is : /home/ubuntu/program/src/dir1/dir2a/dir4
message2-> dirname is : /home/ubuntu/program/src/dir1/dir2
message2-> dirname is : /home/ubuntu/program/src/dir1/dir2/dir3
message1-> total number of files is 20
message2 -> filename is :/home/ubuntu/program/dest/test2aa.txt 3947
message2 -> filename is :/home/ubuntu/program/dest/td>
    <td align="right">
     <span style="font-size: 9pt">
     <a href="text_compare_alignment.html"
        onmouseover="document.images.prev.src='prev-hot.png'"
        onmouseout="document.images.prev.src='prev.png'"
        ><img name=prev src="prev.png" bord
failed to open to append
: File name too long
ubuntu@ubuntu:~/program$
0
 
BeginToLearnAuthor Commented:
you can take a look at my code after dinner meal :)  . Tks a lot . I attach the latest server.c
I need to cook too.
server.c
0
 
sarabandeCommented:
why does message1 arrive after 5 message2?

is the first output from client?

Sara
0
 
sarabandeCommented:
can you printf the full message2 and sizedata Server::receiveOneFile?

Sara
0
 
sarabandeCommented:
also please uncomment the printf in client.c before sending message2.

Sara
0
 
BeginToLearnAuthor Commented:
let me run it. hold on
0
 
BeginToLearnAuthor Commented:
message2-> dirname is : /home/ubuntu/program/src/dir1
message2-> dirname is : /home/ubuntu/program/src/dir1/dir2a
message2-> dirname is : /home/ubuntu/program/src/dir1/dir2a/dir4
message2-> dirname is : /home/ubuntu/program/src/dir1/dir2
message2-> dirname is : /home/ubuntu/program/src/dir1/dir2/dir3
message1-> total number of files is 20
Full message2 is: /home/ubuntu/program/src/dir1/dir2a/test2aa.txt 3947
sizedata is : 52
message2 -> filename is :/home/ubuntu/program/dest/test2aa.txt 3947
Full message2 is: ┬┐<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!-- saved from url=(0029)http://www.helpandmanual.com/ -->
<head>
   <title>Text Compare Replacements</title>
   <meta name="generator" content="Help &amp; Manual" />
   <meta name="keywords" content="replacements,text replacements" />
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <meta http-equiv="Content-Style-Type" content="text/css" />
   <link type="text/css" href="default.css" rel="stylesheet" />
<script type="text/javascript" src="helpman_topicinit.js"></script>
<!-- Redirect browser to frame page if page is not in the content frame. -->
<script type="text/javascript">
<!--
if (location.search.lastIndexOf("toc=0")<=0) {
  if (parent.frames.length==0) { parent.location.href="index.html?text_compare_replacements.html"; }
  else { parent.quicksync('a3.3.3.4'); }
}
//-->
</script>
<script type="text/javascript" src="highlight.js"></script></head>
<body style="margin: 0px 0px 0px 0px; background: #FFFFFF;" onload="highlight();">
<div id="hmpopupDiv" style="visibility:hidden; position:absolute; z-index:1000; "></div>


<table width="100%" border="0" cellspacing="0" cellpadding="5"
       style="margin: 0px; background: #B01213 url(red-bg.jpg) -12px 0px repeat-y;">
<!-- <table width="100%" border="0" cellspacing="0" cellpadding="5" bgcolor="#B01213"> -->
  <tr valign="middle">
<!--    <td style="width: 10px">&nbsp;</td> -->
    <td align="left">
      <p class="p_Heading1"><span class="f_Heading1">Text Compare Replacements┬á </span><img src="pro.png" width="27" height="12" border="0" alt=""></p>
<p class="p_Command2"><span class="f_Command2">Session &gt; Session Settings &gt; Replacements</span></p>

    </td>
    <td align="right">
     <span style="font-size: 9pt">
     <a href="text_compare_alignment.html"
        onmouseover="document.images.prev.src='prev-hot.png'"
        onmouseout="document.images.prev.src='prev.png'"
        ><img name=prev src="prev.png" bord
sizedata is : 2048
message2 -> filename is :/home/ubuntu/program/dest/td>
    <td align="right">
     <span style="font-size: 9pt">
     <a href="text_compare_alignment.html"
        onmouseover="document.images.prev.src='prev-hot.png'"
        onmouseout="document.images.prev.src='prev.png'"
        ><img name=prev src="prev.png" bord
failed to open to append
: File name too long
ubuntu@ubuntu:~/program$


Oh the first five message2 indicate about directories.
0
 
BeginToLearnAuthor Commented:
in dest all subdirectories were created in wrong hierarchy. Let me check those directory path processing
0
 
BeginToLearnAuthor Commented:
I go to work now until midnight.if u see anything wrong,pleas post it.
0
 
ssnkumarCommented:
In the method: pareseFileName(),
you are taking the whole string.
This string contains the filename and also the size of file.
You have to strip off the size of file and use only the filename to create the file.
0
 
ssnkumarCommented:
So, when you call pareseFileName(), the itssize has to get back the size of the file.
This is not happening.
0
 
ssnkumarCommented:
Because of this, the loop:
while( nread < itssize )
is never entered.
0
 
sarabandeCommented:
to add to ssnkumar's comments:

the old version had

itssize = atoi(filesize);

Open in new window

what you didn't do in the later versions.

Sara
0
 
BeginToLearnAuthor Commented:
iam going home now.i honna try in 36"
0
 
ssnkumarCommented:
Since you are writing and reading only strings and integers,
it is better to use fscanf() and fprintf() instead of read/write and send/recv.
read/write and send/recv will work on binary data.
fscanf/fprintf will work on formatted data and it is better to use these functions when you are using strings and integers.
0
 
BeginToLearnAuthor Commented:
back to action. I am reading all comments now.
0
 
sarabandeCommented:
i don't agree to the usage of fscanf and fprintf in c++. formatted i/o using those functions is error-prone and not type-safe. it also can used by malware to overtake a program.

begintolearn, you might think on rename your .c files into .cpp. otherwise, you often will get suggestions to use techniques which might be right for a c program but are not recommended in a c++ environment.

Sara
0
 
BeginToLearnAuthor Commented:
here is my latest server. The directory hierarchy is wrong.
For general, i think fwrite/fread can handle both binary and text. I will rename after this work.
server.c
0
 
BeginToLearnAuthor Commented:
i see the problem now.
The directory from command line is the same during my code . So when the directory from message come, it use dirname in private as the reference. So all directory will be flat.
0
 
BeginToLearnAuthor Commented:
I think of this approach:
  + save the directory path  from command line in a string
  + initilize dirname  with directory path by constructor
  + when call createDir, use dirname as reference ; then update dirname
  + after done createDir, restore the original directory path from commandline to dirname. now work with files.

 how do u think?
0
 
BeginToLearnAuthor Commented:
i gonna go to sleep now because it's 1:26 am . sorry.
0
 
sarabandeCommented:
you could send relative directory paths from client. then you could add the paths to that you got from commandline. for example send src/dir1 from client and then you could add it to /home/ubuntu/programs/server1/ at the server (1) side.

Sara
0
 
BeginToLearnAuthor Commented:
I will think about it carefully in the morning. tks.
0
 
ssnkumarCommented:
sarabande> begintolearn, you might think on rename your .c files into .cpp. otherwise, you often will get
sarabande> suggestions to use techniques which might be right for a c program but are not
sarabande> recommended in a c++ environment
Just by changing from .c to .cpp will not change anything.
Inside the program, there are calls to send(), recv(), write(), etc.
Without changing these, it will still be C code only.
Since he/she is already using these C calls, I suggested fscanf() and fprintf().

sarabande> formatted i/o using those functions is error-prone and not type-safe
I don't agree with this statement.
Can you please give me examples, where it is going to be error prone?
If you use same data type to send and recv the parameters on both server and client, then there will be no problem with type also.
0
 
sarabandeCommented:
do you want to say the variable arguments are type-safe? isn't it that you can get severe errors when typing a wrong number of arguments or wrong argument type not fitting to format specifiers for both scanf and printf where you don't get any hint of compiler? by manipulating the format specifier string a mal program can produce buffer overflow or manipulate return addresses in stack.

send/recv are not the same as printf/scanf. i don't argue against using c runtime calls especially not where there is no c++ alternative. i don't see any reason in the above program to use fprintf and fscanf as alternative to the (working) recv and send and the argument it will keep to be c code only when not replacing one c function by another is not logical.

Sara

0
 
ssnkumarCommented:
sarabande> isn't it that you can get severe errors when typing a wrong number of arguments or wrong
sarabande> argument type not fitting to format specifiers
Here below I reproduce what I had mentioned:
"If you use same data type to send and recv the parameters on both server and client, then there will be no problem with type also"

And making a mistake in program (sending wrong number of arguments, etc), can happen with other function calls also and your compiler might not give errors.
So, this point doesn't justify not using fprintf() and fscanf().

sarabande> send/recv are not the same as printf/scanf.
Where did I say, both are same?
I only said, it is convenient to use fprintf() and fscanf().

In this program, every send() call sends 2 values - 1 string and 1 integer.
So, the same has to be received at the other end.
If I use send() and recv(), you will have to take care of splitting the data, rightly checking the end of data, etc.
If you use fprintf() and fscanf(), you don't need to do any splitting of parsing.

I feel, it is more convenient to use formatted read/write such as fprintf() and fscanf().
They are there to make our life easy in these kind of situations.
That is my suggestion.
 It is left to "BeginToLearn" to use it or not.
0
 
BeginToLearnAuthor Commented:
good morning. let me continue.
0
 
BeginToLearnAuthor Commented:
My solution doesn't work. Below is the reason

commandargument : /home/ubuntu/program/dest
message2-> dirname is : /home/ubuntu/program/src/dir1

----> directory path will be /home/ubuntu/program/dest/dir1
update dirname = /home/ubuntu/program/dest/dir1

message2-> dirname is : /home/ubuntu/program/src/dir1/dir2a

----> directory path will be /home/ubuntu/program/dest/dir1/dir2a
update dirname = /home/ubuntu/program/dest/dir1/dir2a

message2-> dirname is : /home/ubuntu/program/src/dir1/dir2a/dir4

----> directory path will be /home/ubuntu/program/dest/dir1/dir2a/dir4
update dirname = /home/ubuntu/program/dest/dir1/dir2a/dir4

message2-> dirname is : /home/ubuntu/program/src/dir1/dir2

----> directory path will be /home/ubuntu/program/dest/dir1/dir2a/dir4
update dirname = /home/ubuntu/program/dest/dir1/dir2a/dir4

message2-> dirname is : /home/ubuntu/program/src/dir1/dir2/dir3
----> directory path will be /home/ubuntu/program/dest/dir1/dir2a/dir4/dir3
  wrong !!!!! it should be /home/ubuntu/program/dest/dir1/dir2a/dir3
update dirname = /home/ubuntu/program/dest/dir1/dir2a/dir4/dir3
0
 
sarabandeCommented:
ssnkumar, the recv cannot be replaced by fscanf cause the above program receives binary data from a tcp/socket. there is a 1st recv call which reads an integer from socket which tells the length of the next message. tcp/ip doesn't guarantee that a message sent with one call was received with one call. instead you must call recv in a loop until the expected length was read. if you don't do it that way you might read only a part of the message. if you try to read more than what was sent, the function finally hangs.

Sara
0
 
sarabandeCommented:
will you try sending relative paths?

Sara
0
 
BeginToLearnAuthor Commented:
I am thinking about your approach now.

>>you could send relative directory paths from client. then you could add the paths to that you got from >>commandline. for example send src/dir1 from client and then you could add it to /home/ubuntu/programs/server1/ at the server (1) side.

what if /home/ubuntu/program/src in client and server has different location such as /home/ubuntu/Desktop?
0
 
BeginToLearnAuthor Commented:
your solution requires sending more info from clien to server? Could you please explain more?
0
 
sarabandeCommented:
no it requires less info.

in client you would drop the /home/ubuntu/program/src and send

   dir1/...
   dir1/dir2a/...
   dir1/dir2a/dir4/...
   dir1/dir2/dir3/...

then you simply could add that to the dest directory.

Sara
0
 
BeginToLearnAuthor Commented:
you mean like this
 . in client , use the command line argument to drop relative path so it can drop /home/ubuntu/program/src
to get
        dir1/..
        dir1/dir2a/....

and the add that with the command argument in server

right :)? if so , i completely forget to use command argument in client hihi
0
 
sarabandeCommented:
good.

in client you need the  /home/ubuntu/program/src to know from where the files should be taken :)

Sara
0
 
BeginToLearnAuthor Commented:
let me implement it now . tks
0
 
BeginToLearnAuthor Commented:
I think about this. I can determine the length of command argument. So i can you substr to drop relative path. Is it a good way to implement it?
0
 
BeginToLearnAuthor Commented:
so far so good. all directories are in order now :) I am reviewing for files to make sure they will transfer to proper directory :) i guess you are leaving office now Sara.
0
 
BeginToLearnAuthor Commented:
After i update my code, it works for one client to one server :)
I am testing the case one client to 2 servers. It doesn't work. It only transfer files form src to dest1; but dest2 is empty  ( i use src, dest1,dest2 while testing)
Could you please take a look at it?
client.c
client.h
server.c
server.h
config
0
 
BeginToLearnAuthor Commented:
let me close this question. Tks for your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 30
  • 14
  • 9
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now