Chapter-5 Network Programming
Chapter-5 Network Programming
)
BSc. CSIT 7th Semester
Chapter - 5
5.2. Socket programming using TCP, Socket programming using UDP, Working
Advanced JAVA Programming
• computers often need to communicate and provide more than one type of
service or to talk to multiple hosts/computers at a time. For example, there
Advanced JAVA Programming
may be multiple ftp sessions, web connections, and chat programs all
running at the same time.
• To distinguish these services, a concept of port s, a logical access point,
represented by a 16-bit number. E.g. 80, 81
• IP address can be thought of as a house address when a letter is sent via
post/snail mail and port number as the name of a specific individual to
whom the letter has to be delivered.
Sockets and Socket-based Communication
•Sockets provide an interface for programming networks
•A socket program written in Java language can
Chapter – 5 : Network Programming (5 Hrs.)
• ServerSocket
• Socket
• A server program creates a
Advanced JAVA Programming
os = new DataOutputStream(client.getOutputStream());
3. Perform I/O or communication with the server:
Receive data from the server: String line = is.readLine();
Send data to the server: os.writeBytes(“Hello\n”);
4. Close the socket when done:
client.close();
UDP SOCKET PROGRAMMING
• Does not guarantee of delivering packet is accomplished by the UDP
protocol which conveys datagram packets.
Chapter – 5 : Network Programming (5 Hrs.)
information contained within that packet. That means, each packet needs
to have destination address and each packet might be routed differently,
and might arrive in any order. Packet delivery is not guaranteed.
• It can be used to read and write data to the specified resource referred by
import java.io.*;
the URL. import java.net.*;
Advanced JAVA Programming
• You may download the latest version of both JavaMail API and JAF from the
official website of Java. After successfully downloading these two, extract
them. Add mail.jar , smtp.jar and activation.jar file in your classpath to be
eligible to send emails.
import javax.activation.*;
import javax.mail.Session; // Set From Field: adding senders email to from field.
import javax.mail.Transport; message.setFrom(new InternetAddress(sender));
{
// Set Subject: subject of the email
public static void main(String [] args) message.setSubject("This is Subject");
{
// email ID of Recipient. // set body of the email.
String recipient = "recipient@gmail.com"; message.setText("This is a test mail");
try { System.out.println("---------------------------------");
//1) get the session object System.out.println("Email Number " + (i + 1));
Properties properties = new Properties(); System.out.println("Subject: " + message.getSubject());
properties.put("mail.pop3.host", pop3Host); System.out.println("From: " + message.getFrom()[0]);
Session emailSession = Session.getDefaultInstance(properties); System.out.println("Text: " + message.getContent().toString());
Advanced JAVA Programming
}
//2) create the POP3 store object and connect with the pop server
POP3Store emailStore = POP3Store) emailSession.getStore(storeType); //5) close the store and folder objects
emailStore.connect(user, password); emailFolder.close(false);
emailStore.close();
//3) create the folder object and open it
Folder emailFolder = emailStore.getFolder("INBOX"); } catch NoSuchProviderException e) {e.printStackTrace();}
emailFolder.open(Folder.READ_ONLY; catch MessagingException e) {e.printStackTrace();}
catch IOException e) {e.printStackTrace();}
}
12/10/2023
Er. Jeewan Rai
The End
22