help needed

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2009
Posts: 2
Reputation: zedame is an unknown quantity at this point 
Solved Threads: 0
zedame zedame is offline Offline
Newbie Poster

help needed

 
0
  #1
Jan 3rd, 2009
i'was asked to develop chat application using c++
anyone can help me finding tutorials/ articles/ walkthrough or what ever i can get that discuss this topic
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: help needed

 
0
  #2
Jan 3rd, 2009
First post!

Read the sticky at the top of the forum?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: help needed

 
0
  #3
Jan 3rd, 2009
I agree with clockowl.
Anyway, when you say you were asked....by who your lecturer. Something tells me you wouldn't be asked to do something like this if you hadn't already been taught most if not all the material you need to do this. I suggest you look at winsock. It might be a good place for you to start...presuming your developing on windows and for windows OS's of course we don't know these things.

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: help needed

 
1
  #4
Jan 3rd, 2009
Here are two examples I created and use as a reference, maybe they are of help:

  1. /*
  2. ** Playing around with WinSockets
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <winsock.h>
  7.  
  8. int main (int argc, char **argv){
  9. WORD wsaVersion;
  10. wsaVersion = MAKEWORD(2,2);
  11. WSADATA wsaData;
  12. SOCKET sock1;
  13. int wsaerr;
  14.  
  15.  
  16. wsaerr = WSAStartup(wsaVersion, &wsaData);
  17. if (wsaerr != 0){
  18. printf("\nWinsock not found");
  19. return -1;
  20. }
  21. printf("Winsock found");
  22.  
  23. sock1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  24. if (sock1 == INVALID_SOCKET){
  25. printf("Initializing socket failed: %ld\n", WSAGetLastError());
  26. WSACleanup();
  27. return -1;
  28. }
  29. printf("\nSocket() works");
  30.  
  31. WSACleanup();
  32. return 0;
  33. }

  1. #include <stdio.h>
  2. #include <winsock2.h>
  3.  
  4. int main(int argc, char **argv){
  5. WORD wsaVersion = MAKEWORD(2,2);
  6. WSADATA wsaData;
  7. int wsaerr;
  8.  
  9. wsaerr = WSAStartup(wsaVersion, &wsaData);
  10. if (wsaerr){
  11. printf("WSAStartup failed, exiting.");
  12. return -1;
  13. }
  14. SOCKET somesock;
  15. somesock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  16.  
  17. if (somesock == INVALID_SOCKET){
  18. printf("Socket() failed, exiting.");
  19. WSACleanup();
  20. return -1;
  21. }
  22.  
  23. struct sockaddr_in service;
  24. service.sin_family = AF_INET;
  25. service.sin_addr.s_addr = inet_addr("127.0.0.1");
  26. service.sin_port = htons(12344);
  27. if (bind(somesock, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR){
  28. printf("Bind() failed, exiting.");
  29. closesocket(somesock);
  30. WSACleanup();
  31. return -1;
  32. }
  33.  
  34. closesocket(somesock);
  35. WSACleanup();
  36. return 0;
  37. }

Here's are tiny server/client applications. I don't guarrantee they work or are the holy grail of winsock programming, but if they still work then they are good references. ;-)

basicServer.c
  1. /*BasicServer.c*/
  2.  
  3. #include <stdio.h>
  4. #include <winsock.h>
  5. #include <string.h>
  6.  
  7. #define BUFFERSIZE 511
  8.  
  9. int main(int argc, char **argv){
  10. if (argc < 3){
  11. printf("Usage: %s <message to send to client> <ip to listen to>\n", argv[0]);
  12. return -1;
  13. }
  14. SOCKET sock, accepsock = SOCKET_ERROR;
  15. WSADATA wsaData;
  16. WORD wsaVersion = MAKEWORD(2,2);
  17. int sizeOf, wsaerr;
  18.  
  19. struct sockaddr_in sockinfo, clientinfo;
  20. sockinfo.sin_family = AF_INET;
  21. sockinfo.sin_addr.s_addr = inet_addr(argv[2]);
  22. sockinfo.sin_port = htons(12344);
  23.  
  24.  
  25. int sentB, recvB = SOCKET_ERROR;
  26. char sendbuf[BUFFERSIZE];
  27. lstrcpy(sendbuf, argv[1]);
  28. char recvbuf[BUFFERSIZE] = "";
  29.  
  30. /*WSAStartup(<WORD req_version>, <*WSADATA>)
  31.   Initializes Ws2_32.dll, aka the winsock dll.*/
  32. if(WSAStartup(wsaVersion, &wsaData)){
  33. printf("Fatal error: WSAStartup: %d", WSAGetLastError());
  34. return -1;
  35. }
  36.  
  37. /*socket(<addr family> (AF_INET), <sock type> (SOCK_STREAM/DGRAM), <protocol> (TCPPROTO_IP));
  38.   Makes the socket a socket with some info as to what type of socket it should be.*/
  39. sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  40. if (sock == INVALID_SOCKET){
  41. printf("\nFatal error: Socket: %d", WSAGetLastError());
  42. closesocket(sock);
  43. WSACleanup();
  44. return -1;
  45. }
  46.  
  47. /*bind(<socket>, <*sockaddr>, sizeof(<sockaddr>));
  48.   Need to call this in order to be able to listen() or connect()*/
  49. if(bind(sock, (SOCKADDR*)&sockinfo, sizeof(sockinfo)) == SOCKET_ERROR){
  50. printf("\nFatal error: Bind: %d", WSAGetLastError());
  51. closesocket(sock);
  52. WSACleanup();
  53. return -1;
  54. }
  55.  
  56. /*listen(<socket>, <backlog>);
  57.   Puts socket in listening state.*/
  58. if (listen(sock, SOMAXCONN) == SOCKET_ERROR){
  59. printf("\nFatal error: Listen: %d", WSAGetLastError());
  60. closesocket(sock);
  61. WSACleanup();
  62. return -1;
  63. }
  64. printf("\nDone: Listening for connections.");
  65.  
  66. sizeOf = sizeof(clientinfo);
  67. while(accepsock == SOCKET_ERROR)
  68. accepsock = accept(sock,(SOCKADDR*)&clientinfo, &sizeOf);
  69. printf("\n %s connected", inet_ntoa(clientinfo.sin_addr));
  70. sock = accepsock;
  71.  
  72. sentB = send(sock, sendbuf, strlen(sendbuf), 0);
  73. if (sentB == SOCKET_ERROR){
  74. printf("\nServer: Error: Send: %d", WSAGetLastError());
  75. }
  76. printf("\nServer: Sent data:\n Size: %d\n Data: \"%s\"", sentB, sendbuf);
  77.  
  78. recvB = recv(sock, recvbuf, BUFFERSIZE, 0);
  79. if (recvB < 0){
  80. printf("\nServer: Error: Recv: %d", WSAGetLastError());
  81. }
  82. else if(recvB == 0){
  83. printf("Connection closed.");
  84. closesocket(sock);
  85. WSACleanup();
  86. }
  87. printf("\nServer: Recieved data:\n Size: %d\n Data: \"%s\"", recvB, recvbuf);
  88.  
  89.  
  90. printf("\nFinished without errors, shutting down");
  91. shutdown((SOCKET)socket, SD_BOTH);
  92. closesocket(sock);
  93. WSACleanup();
  94. return 0;
  95. }

basicClient.c
  1. /*BasicClient.c*/
  2.  
  3. #include <stdio.h>
  4. #include <winsock.h>
  5. #include <string.h>
  6.  
  7. #define BUFFERSIZE 511
  8.  
  9. int main(int argc, char *argv[]){
  10. if (argc < 3){
  11. printf("Usage: %s <message to send to server> <server IP>\n", argv[0]);
  12. return -1;
  13. }
  14. SOCKET sock, accepsock = SOCKET_ERROR;
  15. WSADATA wsaData;
  16. WORD wsaVersion = MAKEWORD(2,2);
  17. int sizeOf, wsaerr;
  18.  
  19. struct sockaddr_in sockinfo;
  20. sockinfo.sin_family = AF_INET;
  21.  
  22. if (argv[2][strlen(argv[2])-1] == '\n')
  23. argv[2][strlen(argv[2])-1] == '\0';
  24. if(isdigit(argv[2][0]))
  25. sockinfo.sin_addr.s_addr = inet_addr(argv[2]);
  26. else return -1;
  27.  
  28. sockinfo.sin_port = htons(12344);
  29.  
  30.  
  31. int sentB, recvB = SOCKET_ERROR;
  32. char sendbuf[BUFFERSIZE];
  33. lstrcpy (sendbuf, argv[1]);
  34. char recvbuf[BUFFERSIZE] = "";
  35.  
  36. /*WSAStartup(<WORD req_version>, <*WSADATA>)
  37.   Initializes Ws2_32.dll, aka the winsock dll.*/
  38. if(WSAStartup(wsaVersion, &wsaData)){
  39. printf("Fatal error: WSAStartup: %d", WSAGetLastError());
  40. return -1;
  41. }
  42.  
  43. /*socket(<addr family> (AF_INET), <sock type> (SOCK_STREAM/DGRAM), <protocol> (TCPPROTO_IP));
  44.   Makes the socket a socket with some info as to what type of socket it should be.*/
  45. sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  46. if (sock == INVALID_SOCKET){
  47. printf("\nFatal error: Socket: %d", WSAGetLastError());
  48. closesocket(sock);
  49. WSACleanup();
  50. return -1;
  51. }
  52.  
  53.  
  54. if(connect(sock, (SOCKADDR*)&sockinfo, sizeof(sockinfo))){
  55. printf("\nClient: Fatal error: Connect: %d", WSAGetLastError());
  56. closesocket(sock);
  57. WSACleanup();
  58. return -1;
  59. }
  60.  
  61. sentB = send(sock, sendbuf, strlen(sendbuf), 0);
  62. if (sentB == SOCKET_ERROR){
  63. printf("\nClient: Fatal error: Send: %d", WSAGetLastError());
  64. closesocket(sock);
  65. WSACleanup();
  66. return -1;
  67. }
  68. printf("\nClient: Sent data:\n Size: %d\n Data: \"%s\"", sentB, sendbuf);
  69.  
  70. recvB = recv(sock, recvbuf, BUFFERSIZE, 0);
  71. if (recvB < 0){
  72. printf("\nClient: Error: Recv: %d", WSAGetLastError());
  73. }
  74. else if(recvB == 0){
  75. printf("Connection closed.");
  76. closesocket(sock);
  77. WSACleanup();
  78. }
  79. printf("\nClient: Recieved data:\n Size: %d\n Data: \"%s\"", recvB, recvbuf);
  80.  
  81.  
  82. printf("\nFinished without errors, shutting down");
  83. shutdown((SOCKET)socket, SD_BOTH);
  84. closesocket(sock);
  85. WSACleanup();
  86. return 0;
  87. }

Don't forget to link with the winsocket library! (in MinGW, that's ws2_32.a, pass the parameter: -lws2_32 when compiling)
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 2
Reputation: zedame is an unknown quantity at this point 
Solved Threads: 0
zedame zedame is offline Offline
Newbie Poster

Re: help needed

 
0
  #5
Jan 3rd, 2009
Originally Posted by Freaky_Chris View Post
I agree with clockowl.
Anyway, when you say you were asked....by who your lecturer.
for your info i'm working with Java technology for about three years but now i have to stick with c++ for some issues so instead of running blind i was asking for guidance...i need to know the basic idea of how to implement applications like IM in C++...i'm not askin for "Gimme some code plz" thing...
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: help needed

 
0
  #6
Jan 3rd, 2009
The concept is exactly the same, server/client or client/client it really is upto you. Lets say you go for server/client the more likely. Y9ou have the server set up to listen for connections and spawn new threads to deal with no connections recieved and forward data from an incoming socket to another outbound socket. The client will send a socket connection request to the servers IP address once connect is then free to recieve and send information as desired.

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 319 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC