Java Programming Home Page: Archive: Message #54

Date: Mar 13 2000 09:33:24 EST
From: "Java Programming" <javaProgramming-owner@listbot.com>
Subject: reading and writing to a socket

Hi all, 
reading and writing strings to a socket can
be done, but not with bufferedreaders and writers.
The cross platform way to do this, for example to a 
POP3 server (for mail services) is:
// to recv a reply from the pop3 server:
String recv(InputStream is) throws IOException {
	String result = "";
	int c = is.read();
	while (c >=0 && c != '\n') {
		if (c != '\r') {
			result += (char)c;
		}
	}
	return result;
}
Note that the use of \n and \r are needed to keep
platform specific issues from cropping up. Readln on a mac
is different from readln on a windows system!!! And both are
different from readln on a unix system.

// to send a string to a pop3 server:
void send(OutputStream os, String s) throws IOException {
	for (int i=0; i < s.length(); i++) 
		os.write((byte)s.charAt(i));
	os.flush();
}

Regards,
 - DL