| | |
Connecting to secure server with client
![]() |
•
•
Join Date: May 2009
Posts: 2
Reputation:
Solved Threads: 0
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.
C Syntax (Toggle Plain Text)
#include <arpa/inet.h> #include <errno.h> #include <math.h> #include <netdb.h> #include <netinet/in.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/ipc.h> #include <sys/sem.h> #include <sys/shm.h> #include <sys/socket.h> #include <sys/time.h> #include <sys/types.h> #include <sys/stat.h> #include <time.h> #include <unistd.h> #include <values.h> int errno; /* Required for perror and error returned values */ int main(int argc, char **argv) { int socknum, i, z, *myipaddr, numconns, newpid, done, received, recvlen; int msglen, msglen2, retval, conn, r; int msgnum; unsigned int remoteaddrlen; char hostname[80]; unsigned int integer[11]; struct sockaddr_in mysockaddr; struct sockaddr_in remoteaddr; struct hostent *myhostent = 0; unsigned short int *sbuf = (unsigned short int *)integer; sbuf[0] = htons(44); sbuf[1] = htons(0); for (z=1;z<11;z++){ scanf("%d",&(integer[z])); integer[z] = htonl(integer[z]); } /* Open main socket */ socknum = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (socknum == -1) { perror("Socket creation failed"); return -1; } mysockaddr.sin_family = AF_INET; mysockaddr.sin_port = htons(53432); myhostent = gethostbyname("/*the host name*/"); if (myhostent == 0) { perror("gethostbyname failed"); return -2; } /* Set up so address can be reused quickly after socket is taken down */ /* Otherwise, annoying wait reqired after each server deactivation */ i = 1; if (setsockopt(socknum, SOL_SOCKET, SO_REUSEADDR, (void *)&i, sizeof(int))==-1) { perror("setsockopt failed"); return -99; } myipaddr = (int *)myhostent->h_addr_list[0]; mysockaddr.sin_addr.s_addr = *myipaddr; // Could be IN_ADDR_ANY /* Make connection */ retval = connect(socknum, (struct sockaddr*)&mysockaddr, sizeof(struct sockaddr)); if (retval == -1) { perror("Connection failed"); return -3; } /* Handle messages from remote */ retval = send(socknum,integer,44,0); if (retval == -1) { perror("Send failed"); return -4; } retval = recv(socknum,sbuf,sizeof(unsigned short int)*2,0); if (retval <= 0) { perror("Recv failed"); } printf("I got: %d,%d\n", sbuf[0], sbuf[1]); // Print the message here retval = recv(socknum,integer,sizeof(unsigned int),0); if (retval <= 0) { perror("Recv failed"); } printf("I got for integer: %d\n", *integer); // Print the message here close(conn); return 0; }
Last edited by wakeboarder1335; May 8th, 2009 at 6:30 am.
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.
![]() |
Similar Threads
- Connecting to a SQL Server Database using VB (VB.NET)
- internet explorer doesnt seem to bother loading (Viruses, Spyware and other Nasties)
- DNS Error...Cannot find server when connecting to secure sites (Viruses, Spyware and other Nasties)
Other Threads in the C Forum
- Previous Thread: Compilers compatibility
- Next Thread: Compilation Error
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






