| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Tony
Actually, I just studied a lit bit Visual C++ and tried to write a SSL handshake program (just phase 1- phase2) in a simple way, any body can tell me what's wrong in my code and how to run a program in Visual C++. Thanks a lot!!!!
/*SSLserver*/ 1 #include <stdio.h> /*for printf() and fprintf()*/ 2 #include <sys/socket> /*for socket(), bind(), connect()*/ 3 #include <arpa/inet.h> /*for socket_in and inet_ntoa()*/ 4 #include <stdlib.h> /*for atoi()*/ 5 #include <string.h> /*for memset()*/ 6 #include <unistd.h> /*for close()*/ 7 8 #define MAXPENDING 5 /*maximum oustanding connection requests*/ 9 #define RCVBUFSIZE 32 /*size of receive buffer*/ 10 void DieWithError(char *errormessage); /*Error handling function*/ 11 void HandleTCPclient(int clntsocket); /*TCP client handling function*/ 12 13 int main(int argc, char *argv[]) 14 { 15 int servsock; /*socket descriptor for server*/ 16 int clntsock; /*socket descriptor for client*/ 17 struct sockaddr_in echoservaddr; /*local address*/ 18 struct sockaddr_in echoclntaddr; /*client address*/ 19 unsigned short echoservport; /*server port*/ 20 unsigned int clntlen; /*length of client address data structure*/ 21 char echobuffer[RCVBUFSIZE]; /*Buffer for echo string*/ 22 int recvmsgsize; /*size of received message*/ 23 unsigned long Rc; /*random binary number sent to server*/ /*test for correct number of arguments*/ 24 if (argc !=2) 25 { 26 fprintf(stderr, "usage: %s <servr port>\n", argv[0]); 27 exit(1); 28 } 29 30 echoservport = atoi(argv[1]); /*first arg: local port*/ 31 32 /*create socket for incoming connections*/ 33 if ((servsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) 34 DieWithError("socket() failed"); 35 36 /*construct local address structure*/ 37 memset(&echoservaddr, 0, sizeof(echoservaddr)); /*zero out structure*/ 38 echoservaddr.sin_family = AF_INET; /*internet address family*/ 39 echoservaddr.sin_addr.s_addr = htonl(INADDR_ANY); /*any incoming interface*/ 40 echoservaddr.sin_port = htons(echoservport); /*local port*/ 41 42 /*bind to the local address*/ 43 if (bind(servsock, (struct sockaddr *) &echoservaddr, sizeof(echoservaddr)) < 0) 44 DieWithError("bind()failed"); 45 46 /*mark the socket to it will listen for incoming connection*/ 47 if (listen(servsock, MAXPENDING) < 0 48 DieWithError("listen() failed"); 49 50 for (;;) /*run forever*/ 51 { 52 /*set the size of the in-out parameter*/ 53 clntlen = sizeof(echoclntaddr); 54 55 /*wait for a client to connect*/ 56 if ((clntsock = accept(servsock, (struct sockaddr *) &echoclntaddr, &clntlen)) < 0) 57 DieWithError("accept() failed"); 58 59 /*clntsock is connect to a client! */ 60 61 printf("handling client %s\n", inet_ntoa(echoclntaddr.sin_addr)); 62 63 HandleTCPclient(clntsock); 64 } 65 /*receive client_hello message*/ 66 if (recvmsgsize = recv(clntsocket, echobuffer, RCVBUFSIZE, 0)) < 0) 67 DieWithError("recv()failed"); 68 /*send server_hello message*/ 69 struct serverhello 70 { 71 unsigned short version; 72 int R; 73 unsigned long sessionid; 74 char ciphersuite; 75 char compmeth; 76 } 77 /* see if there is more data to receive*/ 78 if ((recvmsgsize = recv(clntsocket, echobuffer, RCVBUFSIZE, 0)) < 0) 79 DieWithError("recv() failed"); 80 close(clntsocket); /*close client socket*/ 81 } /*SSLclient*/ 1 #include <stdio.h> /*for printf() and fprintf()*/ 2 #include <sys/socket.h> /*for socket(), connect(), send(), and recv()*/ 3 #include <arpa/inet.h> /*for sockaddr_in and inet_addr()*/ 4 #include <stdlib.h> /*for atoi()*/ 5 #include <string.h> /*for memset()*/ 6 #include <unistd.h> /*for close()*/ 7 8 #define RCVBUFSIZE 32 /*size of the receive buffer*/ 9 10 void DieWithError(char *errorMessage); /*Error handling function*/ 11 12 int main(int argc, char *argv[]) 13{ 14 int socket(int PF_INET, int SOCK_STREAM, int IPPROTO_TCP); 15 int sock; /*socket descriptor*/ 16 struct sockaddr_in echoServaddr; /*echo server address*/ 17 unsigned short echoServPort; /*Echo server port*/ 18 char *servIP; /*server IP address (dotted quad)*/ 19 char echoBuffer[RCVBUFFSIZE]; /*Buffer for echo string*/ 20 int bytesrcvd, totalbytesrcvd; /*bytes read in single recv*/ 21 22 if ((argc < 3) || (argc > 4)) /*test for coorect number of argument*/ 23 { 24 fprintf(stderr, "Usage:%s <servIP> <Echo Word> [<Echo Port>]\n",argv[0]); 25 exit(1); 26 } 27 servIP = argv[1]; /*First arg: server IP address (dotted quad)*/ 28 clienthello = argv[2]; /*second arg: hello to send*/ 29 30 if(argc == 4) 31 echoServPort = atoi(argv[3]); /*use given port, if any*/ 32 else 33 echoServPort = 7; /*7 is the well-known port for the echo service*/ 34 35 /*Create a reliable stream socket using TCP*/ 36 if (sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) 37 DieWithError("socket() failed"); 38 39 /*construct the server address structure*/ 40 memset(&echoServaddr, 0, sizeof(echoServaddr)); /*zero out structure*/ 41 echoServaddr.sin_family = AF_INET; /*Internet address family*/ 42 echoServaddr.sin_addr.s_addr = inet_addr(servIP); /*Server IP address*/ 43 echoServaddr.sin_port = htons(echoServPort); /*Server port*/ 45 /*Establish the connection to echo Server*/ 46 if (connect(sock, (struct sockaddr *) &echoServaddr, sizeof(echoServaddr)) < 0) 47 DieWithError("connect() failed"); 48 49 echostringlen = strlen(echostring); /*determine input length*/ 50 51 /*send the client_hello to the server*/ 52 struct clienthello 53 { unsigned short version; 54 int R; 55 unsigned long sessionid; 56 char ciphersuite; 57 char compmeth; 58 } 59 send(sock, clienthello, sizeof(clienthello), 0); 60 61 /*receive the same string back from server*/ 62 totalbytesrcvd = 0; 63 printf("received:"); 64 while (totalbytesrcvd < echobuffer) 65 { 66 /*receive up to the buffer size bytes from the sender*/ 67 if ((bytesrcvd = recv(sock, echoBuffer, RCVBUFFSIZE -1, 0)) <= 0) 68 DieWithError("recv() failed or connection closed prematurely"); 69 totalbytesrcvd += bytesrcvd; /*keep tally of total bytes*/ 70 echoBuffer[bytesRcvd] = '\0'; /*terminate the string*/ 71 printf(echoBuffer); /*print the echo buffer*/ 72 } 73 printf("\n"); /*ptint a final linefeed*/ 74 close(sock); 75 exit(0); 76 } |
![]() |
| Viewing: Tutorialized Forums > Desktop Programming > Visual C > Tony |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|