|
Hi,
1. I have written a very simple socket server.
2. It listens in post 63254.
3. First i did a socket_create, socket_bind, socket_listen so here a connection is listening.
4. Then in a loop i do the socket accpet. so here another listen.
5. the read function reads untill i input exit.
6. after that the resource id by socket_accept closes.
7. and then the main connection closes.
when i checked this process in TCPview after closing all connections i can still see the system process showing TIME_WAIT for post 63254
if i again run the socket server program it is connecting and when one full process is over all the connection is closed and the program terminated and now i can see another TIME_WAIT for the same port. but still i could connect to the same port the third time.
in stackover question answer it is said that connection cannot be done for port which is in wait state.
I opened firefox browser it opened 4 connections. when i closed it all closed and the system process showed 4 time waits for 2 minutes. all time wait stays for 2 minutes and disappears.
so what i conclude is for every connection close the time wait is occurs and cannot be avoided.
i read many posts in stack overflow flow but still wasn't sure of it.
i run the following code in command line.
My server Code
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$str = '';
$buff = '';
$s = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!$s)die('Unable to create socket');
if(!socket_bind($s,'127.0.0.1',63254))
die("\nTrying to Bind: ".socket_strerror(socket_last_error()));
if(!socket_listen($s,1))
die(socket_strerror(socket_last_error()));
while(1)
{
$acc = socket_accept($s);
if(!$acc)die(socket_strerror(socket_last_error()));
if(!$acc)die(socket_strerror(socket_last_error()));
while(1)
{
$str = socket_read($acc,512);
$buff.= $str;
echo $str;
if($str===false)die(socket_strerror(socket_last_error()));
if($str=="exit\r\n")break;
}
socket_close($acc);
if(preg_match('/exit/',$buff))break;
}
socket_close($s);
The client code
set_time_limit(0);
$f = fsockopen('127.0.0.1',63254,$a,$b,10);
if(!$f)die('cannot connect');
echo "\nConnected: \n";
do{
$buff = fgets(STDIN);
fwrite($f,$buff);
}while($buff!="exit\r\n");
fclose($f);
The code is written for php but it is exactly like c so people can understand without doubts. so i posted here.
I would like to have any kind of reply related to my question.
I have also discussed about this earlier in here...
if this still exists then just have a look into it.
http://stackoverflow.com/questions/3811067/socket-program-is-able-to-connect-to-the-port-which-is-still-in-time-wait[^]
http://vikku.info/ Today's Beautiful Moments are
Tomorrow's Beautiful Memories
modified on Wednesday, October 6, 2010 2:57 AM
|
|
|
|
|
Jayapal Chandran wrote: when i checked this process in TCPview after closing all connections i can still see the system process showing TIME_WAIT for post 63254
The server socket will always show LISTENING state while running. Incoming connections, which the server has accepted and then have been closed, might end up having TIME_WAIT state for a while. This is an important part of TCP's reliability, it serves multiple purposes and should never be defeated. A networking book will give you more details why this is important. From a practical point of view, sockets are connections between two endpoints and not both socket will have the TIME_WAIT state, normally only one will enter it, it depends on which endpoint performs the active close. In my network applications I usually design the protocol so that the clients get the TIME_WAIT , by letting the clients close the connection to the server.
On Windows or Linux you can see server and client connections with:
netstat -an
The following shows a chat server, a connected client and a closed client:
Active Connections
Proto Local Address Foreign Address State
TCP 0.0.0.0:9009 0.0.0.0:0 LISTENING
TCP 127.0.0.1:2302 127.0.0.1:9009 ESTABLISHED
TCP 127.0.0.1:2279 127.0.0.1:9009 TIME_WAIT
Here is a little tip, the connection list is easier to read if you run server and clients on two different computers.
Hope this helps!
|
|
|
|
|
ahh, there it is. now i understood about the time wait. nice. thank you.
was that stackoverflow link stating the same like your answer?
just in case if you have read that.
|
|
|
|
|
Jayapal Chandran wrote: was that stackoverflow link stating the same like your answer?
Yes, EJP gives pretty much the same answer. Happy coding!
|
|
|
|
|
That's nice of you. Thank you for your time to check that.
|
|
|
|
|
I have a Process A and a Process B. From Process A i am creating a remoteThread in Process B. Now i want to hook exitProcess function in process B.
I want to hook ExitProcess and write MyExitProcess. In MyExitProcess i want to take a decision whether to exit process B or not.
Can anyone please help me with this..
|
|
|
|
|
|
I'v downloaded "BasicExcel" to hanlder my Excel operation.But it meets some problem.
Sometimes,BasicExcel.Load collapse,it seems to have some relation with the Excel file.The file size is not the reason,because I have tested that.
If that case happened,open the file,then save,and redo BasicExcel.Load ,it's OK!
Does the BasicExcel have some bugs?
|
|
|
|
|
You should post your question in the forum at the end of the article so that the author gets notified.
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
BasicExcel works best with MS Excel 2003.
If problem is not resolved then :
[1]create a new Excel file in MS Excel 2003 format.
[2]copy your content in newly created file.
[3]select all the content and choose Arial as the font type.
[4]save
[5]remove read only flag(if at all applicable)
[6]run the code.
these are the steps I followed when i faced similar problem and it worked.
Sameer();
|
|
|
|
|
Hello,
I am trying to compare 2 dates and find out which one is greater. The second date is current date.
It is not working. Below is full program code :
<br />
#include <stdio.h><br />
#include <conio.h><br />
#include <time.h><br />
<br />
void CompareDates();<br />
<br />
void main()<br />
{<br />
CompareDates();<br />
getch();<br />
}<br />
<br />
<br />
void CompareDates() <br />
{<br />
int year = 2010;<br />
int month = 12;<br />
int day = 31;<br />
<br />
time_t rawtime1;<br />
time(&rawtime1);<br />
time_t rawtime2;<br />
time(&rawtime2);<br />
<br />
struct tm * timeinfo1;<br />
timeinfo1 = localtime(&rawtime1);<br />
struct tm * timeinfo2;<br />
timeinfo2 = localtime(&rawtime2);<br />
<br />
timeinfo1->tm_year = year - 1900;<br />
timeinfo1->tm_mon = month - 1;<br />
timeinfo1->tm_mday = day;<br />
mktime (timeinfo1);
<br />
char buffer1 [80];<br />
strftime (buffer1,80,"timeinfo1 : %c.",timeinfo1);<br />
puts (buffer1);<br />
<br />
char buffer2 [80];<br />
strftime (buffer2,80,"timeinfo2 : %c.",timeinfo2);<br />
puts (buffer2);<br />
<br />
if(timeinfo1 > timeinfo2) <br />
{<br />
puts("more");<br />
}<br />
else <br />
{<br />
puts("less");<br />
}<br />
}<br />
Your guidance is very much appreciated.
Thanking you in advance,
Goutam
|
|
|
|
|
ggoutam7 wrote: if(timeinfo1 > timeinfo2)
You are comparing pointers ("timeinfo1" & "timeinfo2") and not the actual content pointed to by these pointers.
|
|
|
|
|
Since you're using only date and no time, here is something you can do.
Create strings in the format yearmonthday. eg: 20101231.
Now you can do a direct comparison using the comparison operators.
|
|
|
|
|
Hello,
Thank you very much for the guidance of both of you.
I got it solved. Below is the code for the benefit of the readers :
<br />
<br />
#include <conio.h><br />
#include <time.h><br />
#include <atlstr.h><br />
#include <atltime.h><br />
<br />
<br />
void CompareDates();<br />
<br />
void main()<br />
{<br />
CompareDates();<br />
getch();<br />
}<br />
<br />
void CompareDates() <br />
{<br />
int nYear = 2010;<br />
int nMonth = 1;<br />
int nDay = 1;<br />
CString sYear;<br />
CString sMonth;<br />
CString sDay;<br />
long lDate1 = 0;<br />
long lDate2 = 0;<br />
CString sDate1;<br />
CString sDate2;<br />
<br />
sYear.Format("%d",nYear);<br />
<br />
if (nMonth < 10)<br />
sMonth.Format("0%d",nMonth);<br />
else<br />
sMonth.Format("%d",nMonth);<br />
<br />
if (nDay < 10)<br />
sDay.Format("0%d",nDay);<br />
else<br />
sDay.Format("%d",nDay);<br />
<br />
sDate1 = sYear + sMonth + sDay;<br />
<br />
CTime time = CTime::GetCurrentTime();<br />
sDate2.Format("%s%s%s",time.Format("%Y"),time.Format("%m"),time.Format("%d")); <br />
<br />
puts("sDate1 : " + sDate1);<br />
puts("sDate2 : " + sDate2);<br />
<br />
lDate1 = atol(sDate1);<br />
lDate2 = atol(sDate2);<br />
<br />
if(lDate1 >= lDate2) <br />
{<br />
puts("more");<br />
}<br />
else <br />
{<br />
puts("less");<br />
}<br />
}<br />
<br />
with warmest regards,
Goutam
modified on Wednesday, October 6, 2010 4:11 AM
|
|
|
|
|
Or...
void CompareDates( void )
{
int nYear = 2010;
int nMonth = 1;
int nDay = 1;
CString sDate1;
CString sDate2;
sDate1.Format("%d%02d%02d", nYear, nMonth, nDay);
CTime time = CTime::GetCurrentTime();
sDate2.Format("%s%s%s",time.Format("%Y"),time.Format("%m"),time.Format("%d"));
puts("sDate1 : " + sDate1);
puts("sDate2 : " + sDate2);
if (sDate1 >= sDate2)
puts("more");
else
puts("less");
}
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
|
Hi Guys, I am using CDateTimeCtrl in my project.I dont know how to get Date, Month and Year Separatly.
If you Know explain me with simple code
Thanks in Advance
|
|
|
|
|
How about reading the documentation in MSDN and letting us know what you did not understand after seeing the member functions of this class to get the date, month and year separately?
Read GetTime function and refer the SYSTEMTIME structure.
I am a HUMAN. I have that keyword (??? too much) in my name........
_AnsHUMAN_b>
|
|
|
|
|
|
Use the method "GetTime" and then Format the CTime-object.
Example to get the month:
CTime timeTime;
theDateTimeCtrl.GetTime(timeTime);
CString strMonth(timeTime.Format(_T("%m"));
Now the Month is in the string.
Look in the documentation of strftime to get the formatting codes.
|
|
|
|
|
hi,i'm currently doing a project using a software,Waspmote. I have some problems with my codings,it gave me 2 errors when i tried correcting it,
In function 'void loop()':
error: expected initializer before 'uint8_t'
In function 'void loop()':
error: a function-definition is not allowed here before '{' token
could anyone kindly help me take a look at the codings and suggest ways for me to improve on it.The code is as shown below:
int n=0;
void setup(){
// setup for Serial port over USB
USB.begin();
USB.println("USB port started...");
USB.close();
// Powering BT
BT.ON();
// Init
if(!BT.init()) USB.println("Init OK");
}
void loop()
{
// Searching for devices
if(!BT.scanNetwork()) USB.println("Scan OK");
else USB.println("Scan failed");
// Printing the devices that have been found
for(int l=0;l<BT.devices_found;l++)
{
n=0;
USB.print("---- DEVICE ");
USB.print(l,DEC);
USB.print(" ---- ");
USB.print("MAC: ");
for(int m=0;m<12;m++)
{
USB.print(BT.discovered_devices[l].mac_address[m],BYTE);
}
USB.print(" -- NAME: ");
while(BT.discovered_devices[l].name[n]!='\0')
{
USB.print(BT.discovered_devices[l].name[n],BYTE);
n++;
}
n=0;
USB.print(" -- CoD: ");
while(BT.discovered_devices[l].CoD[n]!='\0')
{
USB.print(BT.discovered_devices[l].CoD[n],BYTE);
n++;
}
USB.println("");
delay(2000);
}
// Creating a connection to a device
if(!BT.createConnection("0017E5F1CF74","02"))
{
USB.println("");
USB.print("--- CONNECTION -- MTU: ");
USB.print(BT.connection_mtu[0],BYTE);
USB.print(BT.connection_mtu[1],BYTE);
USB.println(BT.connection_mtu[2],BYTE);
}
else USB.println("Connection failed");
delay(1000);
void printData(char* data )
{
uint8_t uart=0;
printString(data,uart);
printByte('\r',uart);
printByte('\n',uart);
}
delay(2000);
return ;
}
|
|
|
|
|
Check where you put your braces { }. It looks to me like your "printData()" function is INSIDE "loop()". Indenting your code neatly NEVER hurts and often (in cases like this) helps you see what you've written.
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
thanks for the reply.I tried removing the brackets but it still doesn't work,if possible may i just trouble you to help me correct the codings because i'm rather new in programming.
|
|
|
|
|
I didn't say remove the braces. You need to get them matching correctly, like this:
func1() {
...
...
}
func2() {
...
...
}
What you had is more like
func1() {
...
...
func2() {
...
...
}
}
See how the indentation makes it easy to spot the problem?
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
After looking through the codes and compiling, I still get an error:
In function 'void loop()':
error: a function-definition is not allowed here before '{' token
int n=0;
void setup()
{
USB.begin();
USB.println("USB port started...");
USB.close();
BT.ON();
if(!BT.init()) USB.println("Init OK");
}
void loop()
{
if(!BT.scanNetwork()) USB.println("Scan OK");
else USB.println("Scan failed");
for(int l=0;l<BT.devices_found;l++)
{
n=0;
USB.print("---- DEVICE ");
USB.print(l,DEC);
USB.print(" ---- ");
USB.print("MAC: ");
for(int m=0;m<12;m++)
{
USB.print(BT.discovered_devices[l].mac_address[m],BYTE);
}
USB.print(" -- NAME: ");
while(BT.discovered_devices[l].name[n]!='\0')
{
USB.print(BT.discovered_devices[l].name[n],BYTE);
n++;
}
n=0;
USB.print(" -- CoD: ");
while(BT.discovered_devices[l].CoD[n]!='\0')
{
USB.print(BT.discovered_devices[l].CoD[n],BYTE);
n++;
}
USB.println("");
delay(2000);
}
if(!BT.createConnection("0017E5F1CF74","02"))
{
USB.println("");
USB.print("--- CONNECTION -- MTU: ");
USB.print(BT.connection_mtu[0],BYTE);
USB.print(BT.connection_mtu[1],BYTE);
USB.println(BT.connection_mtu[2],BYTE);
}
else USB.println("Connection failed");
delay(1000);
void printData(char* data )
{
uint8_t uart=0;
printString(data,uart);
printByte('\r',uart);
printByte('\n',uart);
}
delay(2000);
return ;
}
|
|
|
|
|