943,796 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 484
  • C++ RSS
Jan 3rd, 2009
0

help needed

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zedame is offline Offline
2 posts
since Jan 2009
Jan 3rd, 2009
0

Re: help needed

First post!

Read the sticky at the top of the forum?
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Jan 3rd, 2009
0

Re: help needed

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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Jan 3rd, 2009
1

Re: help needed

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)
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Jan 3rd, 2009
0

Re: help needed

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zedame is offline Offline
2 posts
since Jan 2009
Jan 3rd, 2009
0

Re: help needed

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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Sudoku
Next Thread in C++ Forum Timeline: Which function is better for launching external application?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC