Connecting to secure server with client

Reply

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

Connecting to secure server with client

 
0
  #1
May 8th, 2009
Hello, I was wondering how I could connect to a server using the client that I wrote below, that requires a username and password. I'm a little confused when it comes to reading the man pages.

  1. #include <arpa/inet.h>
  2. #include <errno.h>
  3. #include <math.h>
  4. #include <netdb.h>
  5. #include <netinet/in.h>
  6. #include <signal.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/ipc.h>
  11. #include <sys/sem.h>
  12. #include <sys/shm.h>
  13. #include <sys/socket.h>
  14. #include <sys/time.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19. #include <values.h>
  20.  
  21. int errno; /* Required for perror and error returned values */
  22.  
  23. int main(int argc, char **argv) {
  24.  
  25. int socknum, i, z, *myipaddr, numconns, newpid, done, received, recvlen;
  26. int msglen, msglen2, retval, conn, r;
  27. int msgnum;
  28. unsigned int remoteaddrlen;
  29. char hostname[80];
  30. unsigned int integer[11];
  31. struct sockaddr_in mysockaddr;
  32. struct sockaddr_in remoteaddr;
  33.  
  34. struct hostent *myhostent = 0;
  35.  
  36. unsigned short int *sbuf = (unsigned short int *)integer;
  37. sbuf[0] = htons(44);
  38. sbuf[1] = htons(0);
  39. for (z=1;z<11;z++){
  40. scanf("%d",&(integer[z]));
  41. integer[z] = htonl(integer[z]);
  42. }
  43. /* Open main socket */
  44. socknum = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  45.  
  46. if (socknum == -1) {
  47. perror("Socket creation failed");
  48. return -1;
  49. }
  50.  
  51. mysockaddr.sin_family = AF_INET;
  52. mysockaddr.sin_port = htons(53432);
  53.  
  54. myhostent = gethostbyname("/*the host name*/");
  55. if (myhostent == 0) {
  56. perror("gethostbyname failed");
  57. return -2;
  58. }
  59.  
  60. /* Set up so address can be reused quickly after socket is taken down */
  61. /* Otherwise, annoying wait reqired after each server deactivation */
  62. i = 1;
  63. if (setsockopt(socknum, SOL_SOCKET, SO_REUSEADDR, (void *)&i, sizeof(int))==-1) {
  64. perror("setsockopt failed");
  65. return -99;
  66. }
  67.  
  68. myipaddr = (int *)myhostent->h_addr_list[0];
  69. mysockaddr.sin_addr.s_addr = *myipaddr; // Could be IN_ADDR_ANY
  70.  
  71. /* Make connection */
  72. retval = connect(socknum, (struct sockaddr*)&mysockaddr, sizeof(struct sockaddr));
  73. if (retval == -1) {
  74. perror("Connection failed");
  75. return -3;
  76. }
  77.  
  78. /* Handle messages from remote */
  79.  
  80. retval = send(socknum,integer,44,0);
  81. if (retval == -1) {
  82. perror("Send failed");
  83. return -4;
  84. }
  85.  
  86.  
  87.  
  88. retval = recv(socknum,sbuf,sizeof(unsigned short int)*2,0);
  89. if (retval <= 0) {
  90. perror("Recv failed");
  91. }
  92.  
  93. printf("I got: %d,%d\n", sbuf[0], sbuf[1]); // Print the message here
  94.  
  95. retval = recv(socknum,integer,sizeof(unsigned int),0);
  96. if (retval <= 0) {
  97. perror("Recv failed");
  98. }
  99.  
  100. printf("I got for integer: %d\n", *integer); // Print the message here
  101.  
  102. close(conn);
  103. return 0;
  104. }
Last edited by wakeboarder1335; May 8th, 2009 at 6:30 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Connecting to secure server with client

 
0
  #2
May 8th, 2009
After connecting to the server there is probably some sort of communication protocol strings the client has to pass to the server in order for the server to authenticate it. What strings that is will depend on the server. You will have to read the server's documentation to find out how to do it.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC