To make it portable to all C compilers you probably need to
serialize into byte array for each member separately.
Or just invoke compiler with option not to use padding (but this will apply for all structs). In any case, there is performance hit. Or you can use compiler dependent feature to disable padding from source code, as in upper example or sometimes #pragma.
Depends on what you're going to do with it. On many machines, if you try to use length, you'll probably get a SIGBUS or Bus error.
If you're serializing for the purposes of transferring to another machine, then you need to do each member separately, especially if the receiving machine is a completely different architecture.
If it is the same architecture, I wouldn't bother with unpadding it
I have developed a client server application using C.
i have a list of usernames and a list of passwords in a text file. i need to be able to do 2 loops so i can send the usernames and the passwords to the server and get a response back. basically its a ftp force attack to find the correct username and password. i have written most of the code but i'm kind of strugling to finish it off and sort out the errors. this is a task we have to do for university. i have tried different books and websites and still can't sort out the errors. i really need your help.
Any advice would be most helpfull
this is the client part:
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define BUFSIZE 4096 /* Size of receive buffer */
void DieWithError(char *errorMessage); /* Error handling function */
void handeltcpclient(int sock, char *argv[]); /* Server handling function */
int main(int argc, char *argv[])
{
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
unsigned short echoServPort; /* Echo server port */
char *servIP; /* Server IP address (dotted quad) */
if ((argc < 2) || (argc > 3)) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s <Server IP> [<Port>]\n",argv[0]);
exit(1);
}
servIP = argv[1]; /* First arg: server IP address (dotted quad) */
if (argc == 3)
echoServPort = atoi(argv[2]); /* Use given port, if any */
else
echoServPort = 21; /* 7 is the well-known port for the echo service */
/* Create a reliable, stream socket using TCP */
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("socket() failed");
/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */
/* Establish the connection to the echo server */
if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError("connect() failed");
handeltcpclient(sock, argv);
close(sock);
}
this is the handeltcpclient.c
#include <stdio.h> /* for printf() and fprintf() */
#include <string.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for recv() and send() */
#include <unistd.h> /* for close() */
response = strncpy(&srvBuf, 3);
result = strcmp( result, "230");
if( result == 0)
{
sprintf(&clntBuf, "USER %s", USER);
send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf));
DieWithError("send() sent a different number of bytes than expected");
response = strncpy(&srvBuf, 3);
result = strcmp( result, "331");
if( result == 0)
{
sprintf(&clntBuf, "PASS %s", PASS);
send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf));
DieWithError("send() sent a different number of bytes than expected");
result = strncpy(&srvBuf, 3);
result = strcmp(result, "230");
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.