Sie sind auf Seite 1von 6

http://arantxa.ii.uam.es/~ortigosa/poo/05-java4.

pdf
http://arantxa.ii.uam.es/~castells/docencia/poo/9-concurrencia.pdf
http://www.info-ab.uclm.es/asignaturas/42579/pdf/02-Capitulo2b.pdf
http://www.info-ab.uclm.es/asignaturas/42579/pdf/
http://www.javapractices.com/topic/TopicAction.do?Id=42
http://www.leepoint.net/notes-java/io/10file/10readfile.html
http://www.abbeyworkshop.com/howto/java/writeText/index.html
http://www.kodejava.org/examples/28.html
http://www.tech-recipes.com/rx/1190/java-text-file-io-input-swing/#
http://www.homeandlearn.co.uk/java/read_a_textfile_in_java.html
http://faculty.francis.edu/jlamertina/SFU/cpsc122/slides/Reading%20and%20Writing
%20Text%20Files.ppt
http://code.hammerpig.com/read-from-multiple-text-files-in-java.html
http://www.ezzylearning.com/tutorial.aspx?tid=2768450
http://www.roseindia.net/java/example/java/applet/readFileApplet.shtml
http://pirate.shu.edu/~wachsmut/Teaching/CSAS2214/Virtual/Lectures/lecture16.html
http://www.java-forums.org/new-java/7001-reading-text-file-into-array.html
http://stackoverflow.com/questions/285712/java-reading-a-file-into-an-array
http://stackoverflow.com/questions/4146996/javaswing-filereader-read-txt-file-and-write-
into-jlist-randomly-works-oo
http://www.coderanch.com/t/343157/GUI/java/reading-text-file-GUI
http://answers.yahoo.com/question/index?qid=20090113231210AAHeZ5Q
http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/data.html
http://www.dreamincode.net/forums/topic/219722-read-in-file-and-store-in-2d-array/
http://studentoffortune.com/question/600692/Java-GUI-employee-array
http://www.java-tips.org/java-me-tips/midp/reading-and-writing-array-data-to-persistent-
storage.html revisar este.

http://www.leepoint.net/notes-java/index.html
http://www.javaguicodexample.com/javaprogramexampleusejgrasp2.html
http://www.kishwaukeecollege.edu/faculty/dklick/cis260/programs.html
http://www.dreamincode.net/forums/topic/108833-arrays-and-guis/
http://www.gidforums.com/t-24256.html

Sockets - Presentation Transcript


1. Network Programming and Java Sockets SIVASUBBU B [email_address]
2. Agenda
o Introduction
o Elements of Client Server Computing
o Networking Basics
o Understanding Ports and Sockets
o Java Sockets
 Implementing a Server
 Implementing a Client
o Sample Examples
o Conclusions
3. Introduction
o Internet and WWW have emerged as global ubiquitous media for communication and
changing the way we conduct science, engineering, and commerce.
o They also changing the way we learn, live, enjoy, communicate, interact, engage, etc.
It appears like the modern life activities are getting completely centered around the Internet.
4. Internet Applications Serving Local and Remote Users Internet Server PC client Local Area
Network PDA
5. Internet & Web as a delivery Vehicle
6. Increased demand for Internet applications
o To take advantage of opportunities presented by the Internet, businesses are
continuously seeking new and innovative ways and means for offering their services via the Internet.
o This created a huge demand for software designers with skills to create new Internet-
enabled applications or migrate existing/legacy applications on the Internet platform.
o Object-oriented Java technologies—Sockets, threads, RMI, clustering, Web services--
have emerged as leading solutions for creating portable, efficient, and maintainable large and complex
Internet applications.
7. Elements of C-S Computing Network Request Result a client, a server, and network Client Server
Client machine Server machine
8. Networking Basics
o Applications Layer
 Standard apps
 HTTP
 FTP
 Telnet
 User apps
o Transport Layer
 TCP
 UDP
 Programming Interface:
 Sockets
o Network Layer
 IP
o Link Layer
 Device drivers
o TCP/IP Stack

Application (http,ftp,telnet,…) Transport (TCP, UDP,..) Network (IP,..) Link (device driver,..)

9. Networking Basics
o TCP (Transport Control Protocol) is a connection-oriented protocol that provides a
reliable flow of data between two computers.
o Example applications:
 HTTP
 FTP
 Telnet
o TCP/IP Stack

Application (http,ftp,telnet,…) Transport (TCP, UDP,..) Network (IP,..) Link (device driver,..)

10. Networking Basics


o UDP (User Datagram Protocol) is a protocol that sends independent packets of data,
called datagrams , from one computer to another with no guarantees about arrival.
o Example applications:
 Clock server
 Ping
o TCP/IP Stack

Application (http,ftp,telnet,…) Transport (TCP, UDP,..) Network (IP,..) Link (device driver,..)

11. Understanding Ports


o The TCP and UDP protocols use ports to map incoming data to a particular process
running on a computer.

server P o r t Client TCP TCP or UDP port port port port app app app app port# data Data Packet

12. Understanding Ports


o Port is represented by a positive (16-bit) integer value
o Some ports have been reserved to support common/well known services:
 ftp 21/tcp
 telnet 23/tcp
 smtp 25/tcp
 login 513/tcp
o User level process/services generally use port number value >= 1024
13. Sockets
o Sockets provide an interface for programming networks at the transport layer.
o Network communication using Sockets is very much similar to performing file I/O
 In fact, socket handle is treated like file handle.
 The streams used in file I/O operation are also applicable to socket-based
I/O
o Socket-based communication is programming language independent.
 That means, a socket program written in Java language can also
communicate to a program written in Java or non-Java socket program.
14. Socket Communication
o A server (program) runs on a specific computer and has a socket that is bound to a
specific port. The server waits and listens to the socket for a client to make a connection request.

server Client Connection request port

15. Socket Communication


o If everything goes well, the server accepts the connection. Upon acceptance, the
server gets a new socket bounds to a different port. It needs a new socket (consequently a different
port number) so that it can continue to listen to the original socket for connection requests while
serving the connected client.

server Client Connection port port port

16. Sockets and Java Socket Classes


o A socket is an endpoint of a two-way communication link between two programs
running on the network.
o A socket is bound to a port number so that the TCP layer can identify the application
that data destined to be sent.
o Java’s .net package provides two classes:
 Socket – for implementing a client
 ServerSocket – for implementing a server
17. Java Sockets ServerSocket(1234) Socket(“128.250.25.158”, 1234) Output/write stream Input/read
stream It can be host_name like “mandroo.cs.mu.oz.au” Client Server
18. Implementing a Server 1. Open the Server Socket: ServerSocket server; DataOutputStream os;
DataInputStream is; server = new ServerSocket( PORT ); 2. Wait for the Client Request: Socket client =
server.accept(); 3. Create I/O streams for communicating to the client is = new
DataInputStream( client.getInputStream() ); os = new DataOutputStream( client.getOutputStream() ); 4.
Perform communication with client Receive from client: String line = is.readLine(); Send to client:
os.writeBytes("Hello "); 5. Close sockets: client.close(); For multithreaded server: while(true) { i.
wait for client requests (step 2 above) ii. create a thread with “client” socket as parameter (the thread creates
streams (as in step (3) and does communication as stated in (4). Remove thread once service is provided. }
19. Implementing a Client
o 1. Create a Socket Object:
o client = new Socket( server, port_id );
o 2. Create I/O streams for communicating with the server.
o is = new DataInputStream(client.getInputStream() );
o os = new DataOutputStream( client.getOutputStream() );
o 3. Perform I/O or communication with the server:
 Receive data from the server:
o String line = is.readLine();
 Send data to the server:
o os.writeBytes("Hello ");
o 4. Close the socket when done:
o client.close();
20. A simple server (simplified code)
o // SimpleServer.java: a simple server program
o import java.net.*;
o import java.io.*;
o public class SimpleServer {
o public static void main(String args[]) throws IOException {
o // Register service on port 1234
o ServerSocket s = new ServerSocket(1234);
o Socket s1=s.accept(); // Wait and accept a connection
o // Get a communication stream associated with the socket
o OutputStream s1out = s1.getOutputStream();
o DataOutputStream dos = new DataOutputStream ( s1out );
o // Send a string!
o dos.writeUTF("Hi there");
o // Close the connection, but not the server socket
o dos.close();
o s1out.close();
o s1.close();
o }
o }
21. A simple client (simplified code)
o // SimpleClient.java: a simple client program
o import java.net.*;
o import java.io.*;
o public class SimpleClient {
o public static void main(String args[]) throws IOException {
o // Open your connection to a server, at port 1234
o Socket s1 = new Socket("mundroo.cs.mu.oz.au",1234);
o // Get an input file handle from the socket and read the input
o InputStream s1In = s1.getInputStream() ;
o DataInputStream dis = new DataInputStream(s1In);
o String st = new String ( dis.readUTF() );
o System.out.println(st);
o // When done, just close the connection and exit
o dis.close();
o s1In.close();
o s1.close();
o }
o }
22. Run
o Run Server on mundroo.cs.mu.oz.au
 [raj@mundroo] java SimpleServer &
o Run Client on any machine (including mundroo):
 [raj@mundroo] java SimpleClient
 Hi there
o If you run client when server is not up:
 [raj@mundroo] sockets [1:147] java SimpleClient
 Exception in thread "main" java.net.ConnectException:
Connection refused
 at java.net.PlainSocketImpl.socketConnect(Native Method)
 at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:320)
 at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:133)
 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:120)
 at java.net.Socket.<init>(Socket.java:273)
 at java.net.Socket.<init>(Socket.java:100)
 at SimpleClient.main(SimpleClient.java:6)
23. Socket Exceptions
o try {
o Socket client = new Socket(host, port); handleConnection(client);
o }
o catch( UnknownHostException uhe) { System.out.println(&quot;Unknown host: &quot;
+ host); uhe.printStackTrace();
o }
o catch( IOException ioe) {
o System.out.println(&quot;IOException: &quot; + ioe); ioe.printStackTrace();
o }
24. ServerSocket & Exceptions
o public ServerSocket (int port) throws IOException
 Creates a server socket on a specified port.
 A port of 0 creates a socket on any free port. You can use getLocalPort ()
to identify the (assigned) port on which this socket is listening.
 The maximum queue length for incoming connection indications (a request
to connect) is set to 50. If a connection indication arrives when the queue is full, the connection
is refused.
o Throws:
 IOException - if an I/O error occurs when opening the socket.
 SecurityException - if a security manager exists and its checkListen
method doesn&apos;t allow the operation.
25. Server in Loop: Always up
o // SimpleServerLoop.java: a simple server program that runs forever in a single thead
o import java.net.*;
o import java.io.*;
o public class SimpleServerLoop {
o public static void main(String args[]) throws IOException {
o // Register service on port 1234
o ServerSocket s = new ServerSocket(1234);
o while(true )
o {
o Socket s1=s.accept(); // Wait and accept a connection
o // Get a communication stream associated with the socket
o OutputStream s1out = s1.getOutputStream();
o DataOutputStream dos = new DataOutputStream (s1out);
o // Send a string!
o dos.writeUTF(&quot;Hi there&quot;);
o // Close the connection, but not the server socket
o dos.close();
o s1out.close();
o s1.close();
o }
o }
o }
26. Multithreaded Server: For Serving Multiple Clients Concurrently Server Threads Server Process
Client 1 Process Client 2 Process
o Internet
27. Conclusion
o Programming client/server applications in Java is fun and challenging.
o Programming socket programming in Java is much easier than doing it in other
languages such as C.
o Keywords:

Das könnte Ihnen auch gefallen