开发者

A good way to interpret Socket Responses

开发者 https://www.devze.com 2023-04-12 16:56 出处:网络
I am trying to write a simple mail client in Java with sockets. But I am thinking what is the best way to handle the response from the server? Suppose I have a simple code like the one below:

I am trying to write a simple mail client in Java with sockets. But I am thinking what is the best way to handle the response from the server? Suppose I have a simple code like the one below:

smtpSock = new Socket(mailHost, SMTP_PORT);
inn = smtpSock.getInputStream();
outt = smtpSock.getOutputStream();
in = new BufferedReader(new InputStreamReader(inn));
out = new PrintWriter(new OutputStreamWriter(outt), true);

out.println("AUTH LOGIN " + user.getID());
String response = in.readLine();
//manipulate the response string to check if user ID exists on server
//if ID exists, proceed to password. Else, quit.

out.println("PASS " + user.getPassword());
response = in.readLine();
//manipulate the respons开发者_运维百科e string to check if auth success or not

After I have issued the AUTH LOGIN commmand and given the user ID as the parameter, the server is likely going to give a response. I can get the response to a String. Now, to understand the response and know what to do next, I am thinking that I would manipulate the string by tokenising it and then read the first 3 characters one by one to interpret the response code number. This may sound ok since the response code is only 3 characters.

But then what happens if say the socket is now connecting to a POP3 and is retrieving mails. The response is going to be pretty huge. Is there a good and not too complex method to interpret the response and then put them into a data structure? If parsing the string is the only way, would my idea of tokenising the huge set of String data according to its "textual patterns" a little too naive? Is there a more efficient and faster way of doing this?


POP3 is very different than SMTP, among others it uses different response syntax. SMTP has three letter codes, yes, but I would add that there could be extended status codes too following the simple code. POP3 does not have error codes, only a common negative message. You can parse line by line both of them. POP3 does not preceede each line of the message with a status code of course. If the message is very large then you have to write it into a file while you reveive it. I recommend you to look at the source of Java mail servers, Mireka http://code.google.com/p/mireka/ and Apache James http://james.apache.org/

0

精彩评论

暂无评论...
验证码 换一张
取 消