LAB 12: JAVA Sockets Programming Goals Basics of Networking ...
Recommend Documents
Techniques of Java Programming: Sockets in Java. Manuel Oriol. May 10, 2006.
1 Introduction. Sockets are probably one of the features that is most used in ...
Introduction to Object-Oriented Programming with Java--Wu. Chapter 2 - 1. Image
... Identify the basic components of Java programs. Distinguish two types of ...
Techniques of Java Programming: Java Basics. Manuel Oriol. April 18, 2006. 1
Introduction. Java is the Object-Oriented language of choice for most developers.
from the Java Compiler and Java. JVM. 2.6.6 Write a Short Program. 2.6.7
Programming Projects. 2.6.8 Technical Writing. CHAPTER CONTENTS.
For example, if Process A (which grabbed use of port number 1512)
communicates ... Linux source for udp is around 1200 lines vs. >8000 lines for tcp
.
The term network programming refers to writing programs that execute across ...
The java.net package of the J2SE APIs contains a collection of classes and ...
java.net.InetAddress class. ▫ You get an address by using static methods: ▫ ....
Java Network Programming and Distributed Computing, Reilly & Reilly. ▫.
David Morgan 2003-2011. Socket API network transport data link client program
physical socket API network transport data link server program physical socket ...
Loading⦠Page 1. Whoops! There was a problem loading more pages. Unix-Network-Programming-Volume-1-The-Sockets-Network
Presentation to UCHUG - 1/03/07. G. Skalka ... A computer network is two or
more computers connected ... communicating devices (computer and printer, for ...
The VMware® VMCI Sockets Programming Guide describes how to program
virtual .... On each host, modified Firefox browsers on Windows and Linux virtual
...
you will walk through object-oriented programming by example; learning to use a
simple object .... identifies what code to run whereas messages are more generic.
The receiver .... The inheritance relationships among objects thus form a tree,.
Java Programming Basics: Identifiers, Types, Variables, Operators, and Control
Flow. 2. CSD Univ. of Crete. Fall 2012. So, Where's the Java? • Developers of ...
Jun 22, 1998 ... Jim Kurose, Keith Ross ... made by authors of the book (J. F. Kurose ..... Server: ❑
provides requested service to client. ❑ e.g., Web server sends ...
Advanced Network Programming Lab using Java. Angelos ... Network
Programming Handbook. 2 ... new BufferedReader(new InputStreamReader(
System.in));.
Java programs are portable across operating systems and hardware
environments. Portability is to .... default behavior. The basic structure of an
applet that uses each of these predefined methods is: ... free up system resources
, stop threads.
working knowledge of the basic socket infrastructure. A similar API is available in
many languages; this tutorial uses C programming as a ubiquitous low-level ...
Oct 11, 2005 - Read text from standard-in, and print out in reverse character. # .... The send method has a couple of va
cross-platform Berkeley Sockets Interface. Almost all network tools in Linux and
other. UNIX-based operating systems rely on this interface. This tutorial requires
...
Learn the basics of programming and enough of the Java programming language to start writing your own programs. ⢠Lear
Slide 1. Week 1: Review of Java Programming Basics. Sources: • Chapter 2 in
Supplementary Book (Murach's Java Programming). • Appendix A in Textbook ...
With a large number of bogus Programming: Computer Programming for Beginners: Learn the Basics of Java, SQL & C++ re
ISO/OSI and TCP/IP Reference Model. â Flow and Error Control. â Ethernet, HDLC, PPP. â Internet Protocol (IP), IPv6. â TCP. â Domain Name System ...
This chapter runs through networking fundamentals (e.g. what are the client/
server ... a brief word about some other kinds of Java networking (RMI, Jini,.
LAB 12: JAVA Sockets Programming Goals Basics of Networking ...
Learn the basics of Java sockets programming: creation and usage of sockets;
communication between server/client applications running on different machines.
LAB 12: JAVA Sockets Programming Goals Become familiar with basics of networking: internet architecture, IP addresses, TCP connections, ports. Learn the basics of Java sockets programming: creation and usage of sockets; communication between server/client applications running on different machines on the network.
Basics of Networking Internet Architecture: Suppose you want to transfer a file from an FTP server on one machine to your machine using an FTP client. Let us follow the steps required to initiate such transfer. When you start your FTP client and try to connect to an FTP server on some machine (say, denali.ccs.neu.edu) your FTP client first finds out IP address of the server (using DNS for example; denali’s IP is 129.10.116.200). Then it sends a request to open a connection with denali (this request is sent to port 21 – the default port for FTP protocol). If denali is running an FTP server it will respond to the requests that come on port 21. Denali will figure out where the request came from and will reply with an ACK. We will not go into the specifics of establishing a TCP session; we will only note that server and client eventually find out each other’s IP addresses and ports to use for this connection. Once the connection is established data can be transferred.
Basics of Java Sockets Programming What is a socket? A socket is an end-point of a two-way connection between two applications that communicate over the network. In Java a socket can be represented with an instance of Socket class. The java.net package provides two classes that we are going to use for this lab: Socket (client side of the connection) and ServerSocket (server side). So what is socket programming? Socket programming allows one to write applications that communicate over the network. Each computer has a number of ports, and an application can reside on any of these ports. We say that an application is listening on a particular port if it constantly checks for messages coming from network and destined to this particular port number. Usually server side application will be listening on some well-known port number and reply to client applications attempts to establish communication (web servers – port 80, ssh – port 22, FTP servers – port 21, etc). Relevant Java API reference: http://java.sun.com/j2se/1.4.2/docs/api/java/net/ServerSocket.html http://java.sun.com/j2se/1.4.2/docs/api/java/net/Socket.html http://java.sun.com/j2se/1.4.2/docs/api/java/net/InetAddress.html
Client and Server Examples 1. Take a look at the code for MyServer and MyClient classes (pay attention to comments in the code for explanation). Describe the steps the server will take if it gets a packet from the network destined to port 6070. For now the machine that the clients try to connect to is “localhost”. What happens if you set the variable remote to be something else (say, denali.ccs.neu.edu)? 2. Let’s take a look at what the steps for client side program would be: • • • • •
Open a socket Open an input stream and output stream to the socket Read from and write to the stream according to the server's protocol Close the streams Close the socket
What are the steps that MyClient application takes to connect to the server? Look at the while loop and try to understand what exactly happens in the body of this loop and what has to happen for the program to exit this while loop. 3. Can you figure out how to get the IP address or host name of the remote side of the socket? (Hint: look for System.out lines in the code…). Modify the code for the client program: make it try to connect to an alternative server if the connection for the first one fails. Note that you need to only retry to connect once. If this fails just exit the program. The information about what happens whe n a connection attempt is not successful can be found in the API (hint: Socket class, connect() calls).
Network Collaborative Guessing Game Now let’s see who is fast and skillful in doing network programming in Java! You have an opportunity to take part in a collaborative guessing game over the network. The game proceeds as follows: Each player can log in and get assigned a “lucky number”. The goal of the players is to guess the “lucky numbers” of as many players as possible. These “lucky numbers” are string representations of doubles chosen at random by the server. This is why is not likely one will make a correct random guess. However players can query the server for the “lucky numbers” of other users and submit obtained information. So what will you need to do to play this game? You will have to modify the example code for MyClient.java. Make the program connect to the server and either query the server for “lucky numbers” manually (console input/output) or write a program that will fetch information automatically for you. Here are the steps you will have to go through to play:
0. First you need to find out server’s name and port number. (Ask your TA about server name/IP address; the port will most probably be 6070) 1. To log in send “login:first_last ” to the server, where first_last is your first and last name 2. The server will send you back “replylogin:id” and id will be a string representing an integer indicating your unique id in server’s database (“replylogin:failed” will be sent if error occurs). 3. Now you can send a string “list” to the server to find out who else is logged in. The server will send you back the name of one of the logged in users at random, with some probability the server will send you a string “exit”. If the server sends “exit” your client will have to exit – the game is over for you and you will have to start from scratch (Note: this is not enforced, but please be nice and follow the rules!). 4. If you receive “reply:user_name”, then you can send server “request:user_name” and get the lucky number of user user_name. 5. The server will reply to your request with “replyrequest:user_name:number”. 6. Now you can submit “guess:id:user_name:number” and get your points for the correct guess (id is that unique number that the server sent you when you logged in). Note: all the messages can be converted to strings to be sent over the network. Good luck!
Appendix – Game Protocol Message format - Client: login:first_last – login request list – get name of one of the players request:first_last – get lucky number of player first_last guess:id:first_last :number – submit your guess of user first_last’s lucky number Message Format - Server: reply:error – sent upon reception of incorrectly formatted message replylogin:id:number | replylogin:failed – reply to login reply:user_name | exit – response to “list” message replyrequest:first_last :number | replyrequest:empty – response to request message replyguess:correct | replyguess:incorrect – a reply to submission of your guess Score points: how many users’ numbers you guessed + how many times your number was guessed by others. Disclosure: “points” mentioned above unfortunately have nothing to do with extra credit points. But they are still a great way to impress you peers and professors J