[ale] OT: C not C++ Question
cfowler
cfowler at outpostsentinel.com
Sat Dec 28 23:20:00 EST 2002
I've been working on a little project to replicate the success I've had
in Java (fast programming) to C (slow programming). I've been
replicating the Java classes to C and not C++. But I've come into an
interesting issue where since we have no garbage collector in C that we
must be extremely careful to free all allocated data. All the code in
'liboop' malloc()'s each object.
Socket Object:
// Out Socket Object definition
typedef struct oop_Socket {
struct sockaddr_in inet; // Socket info
int sock;
BOOLEAN connected;
BOOLEAN debug;
BOOLEAN server;
BOOLEAN soReuseAddr;
} Socket;
Example Code:
int
main(int argc, char **argv)
{
Socket *sock;
String *tmp;
InputStream *is;
OutputStream *os;
BufferedInputStream *bis;
String *to = String_new("<chuck at cehuber.org>");
String *from = String_new("<cfowler at opsup.com>");
int code;
if((sock = Socket_new("192.168.1.254", 25)) == NULL)
err("Could not create Socket");
if((Socket_connect(sock)) == -1)
err("Could not make conenction");
//Now we have a conenction lets get out streams
is = Socket_getInputStream(sock);
os = Socket_getOutputStream(sock);
// Create our buffered input stream
bis = BufferedInputStream_new(is);
tmp = BufferedInputStream_readLine(bis);
printf("Got: [%s]\n", String_getChars(tmp));
String_dispose(tmp);
tmp = String_new("Helo localhost.localdomain\r\n");
sendCmd(sock, tmp, &code);
String_dispose(tmp);
tmp = String_new_1("MAIL FROM: %s\r\n", String_getChars(from));
sendCmd(sock, tmp, &code);
String_dispose(tmp);
tmp = String_new_1("RCPT TO: %s\r\n", String_getChars(to));
sendCmd(sock, tmp, &code);
String_dispose(tmp);
tmp = String_new("DATA\r\n");
sendCmd(sock, tmp, &code);
String_dispose(tmp);
InputStream_dispose(is);
is = InputStream_new(NULL);
InputStream_setFd(is, 0); // Set FD to STDIN
bis = BufferedInputStream_new(is);
while(true) {
tmp = BufferedInputStream_readLine(bis);
if(String_startsWith(tmp, ".") == true) {
String_append(tmp, "\r\n");
OutputStream_write_1(os, tmp);
tmp = BufferedInputStream_readLine(
BufferedInputStream_new(
Socket_getInputStream(sock)));
printf("Got: [%s]\n", String_getChars(tmp));
String_dispose(tmp);
break;
}
String_append(tmp, "\r\n");
OutputStream_write_1(os, tmp);
}
// Clean up
String_dispose(to);
String_dispose(from);
BufferedInputStream_dispose(bis);
InputStream_dispose(is);
OutputStream_dispose(os);
Socket_dispose(sock);
}
Since the test code never gets a pointer in the while loop to the
BufferdInputStream that is created it is forever lost memory. Any ideas
on how I can accomplish these tasks. Notice how the above C code looks
like Java :)
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale
More information about the Ale
mailing list