473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

C++/Sockets.h ♣ - glitch or error?

83
Hi, I got a working server, and a working client.
the client and sever send a message to each other, in order.
But after user input (for their name)
The server goes into a for loop for the client messages, and then server automaticly recieves the first message as this symbol: ♣, is this just an error or a glitch?
Aug 30 '08 #1
2 1589
gpraghuram
1,275 Expert 1GB
This should be an error.
How are you accepting the connection on server and how the client sends message sends the message back to server?

Raghu
Aug 31 '08 #2
Adam01
83
Ok,

The server accepts multiple clients, and each client has its own thread.

This is the accept loop in main():
Expand|Select|Wrap|Line Numbers
  1. for(;;){
  2.     sin_size = sizeof their_addr;
  3.     if((clientsocket = accept(serversocket, (struct sockaddr *)&their_addr, &sin_size)) == -1){
  4. message.write("Client Accept Error.");
  5. std::cout << WSAGetLastError(); message.write("\n");
  6.     }
  7. else {message.write("Client connected: ");
  8. message.write(inet_ntoa(their_addr.sin_addr));
  9. message.write("\n");
  10. _beginthread(clientloop, 0, (void*)clientsocket);
  11. }

After the second call to for() is when the problem is caught.
The previous calls to recv are fine, retrieve valid responces.


I added // <---- comments to the main points of the client thread.
and this is the client thread main stuff: (not a loop)

Expand|Select|Wrap|Line Numbers
  1. for(;;){
  2.             std::cout << "::Sending welcome message: ";
  3.             if (send(client[id].clientsocket, "Welcome", 7, 0) == -1)
  4.             {std::cout << "\n!:Could not send message to client\n";
  5.             std::cout << WSAGetLastError(); message.write("\n");}
  6.             else message.write("Sent.\n");
  7. std::cout << "Server: Ping\n";
  8. send(client[id].clientsocket, "-Ping", 5, 0);
  9. if((recv(client[id].clientsocket, client[id].buf, client[id].bufsize, 0)) == -1)
  10. {std::cout << "!:Error recieving pong\n"; break;}
  11. else if(strstr(client[id].buf,"-Pong") == NULL)
  12. { std::cout << "!:Error recieving pong\n";break;}
  13. else
  14. message.write("Client["); message.write(id); message.write("]"); message.write(": Pong.\n");
  15. if((recv(client[id].clientsocket, client[id].clientname, client[id].namesize, 0)) == -1)
  16. {std::cout << "!:Error recieving client name\n"; std::cout << WSAGetLastError(); message.write("\n"); break;}
  17. else { std::cout << "Server: Recieved client name as '" << client[id].clientname << "'.\n"; }
  18. recv(client[id].clientsocket, client[id].buf, client[id].bufsize, 0); // <--- so the server doesnt display the ♣
  19. for(;;){  // <----- message handling loop
  20. if((recv(client[id].clientsocket, client[id].buf, client[id].bufsize, 0)) == -1)
  21. { std::cout << "!:Error recieving client message\n";break;}
  22. else {
  23. if(strstr(client[id].buf,"\\#disconnect")) break;
  24. message.write("["); message.write(id); message.write("]"); message.write(client[id].clientname); message.write(": "); message.write(client[id].buf); message.write("\n");
  25. }
  26. }
  27. break;
  28. }
  29.  
Aug 31 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Dmitry Akselrod | last post by:
Hello everyone, I have a vb.net application that wraps the TCPListener object in a class. The server connects to the local interface and establishes itself on port 9900. It then polls for...
11
by: Ted Mayett | last post by:
OK. Here is a glitch, sorry if this has been mentioned before. This is an erratic glitch. I am now up to three other people besides myself who have been able to see this glitch. It seems it...
4
by: Mike Dole | last post by:
I'm working on a client - server application based on the 'How to Sockets Server and How to Sockets Client' code from the Visual Basic ..NET Resource Kit. Since I want to be able to send 'big...
3
by: Logan McKinley | last post by:
I have a C# program that uses blocking sockets and want to allow the user to stop the server. The problem I am having is the socket blocks on...
15
by: mrpolitics | last post by:
So I'm working with PureIRCD (http://sourceforge.net/projects/pure-ircd) and everything was fine untill yesterday when the server crashed. So I did a cold restart and staretd the server back up...
2
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If...
6
by: Aero | last post by:
Hi, My window application written in C# is throwing following exception while connecting to one FTP location The type initializer for System.Net.Sockets.Socket threw an exception This exe is...
7
by: Adam01 | last post by:
Im using cygwin to test the code of a server I am writing. I've included sys/types.h, sys/socket.h, netdb.h, and arpa/inet.h. And this is the output.. ../../../sockets.cpp: In constructor...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.