Skip to content
UoL CS Notes

Java Socket Programming Recap & Assignment 1

COMP211 Lectures

SMTP

After the header, and before the body, of the email there must be a blank line. This is required for the assignment.

DATA
SUBJECT: Hello

This is the body.
.

Java Sockets

To open a socket use the code:

import java.net.*;
Socket socket = new Socket("ip.address", <port>);

Reading Sockets

To read the socket you can use the following:

import java.io.*
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStremReader(is);
BufferedReader br = new BufferedReader(isr);

The BufferedReader allows you to read the stream a line at a time.

To read the buffer to a variable you can use the following:

String response = br.readLine();

Writing to Sockets

To send data you must first setup an output stream:

DataOutputStream os = new DataOutputStream(socket.getOutputStream());

To write a variable to the stream you use:

os.writeBytes(variable);

Assignment 1

The instructions for this assignment are available here.