[go: up one dir, main page]

0% found this document useful (0 votes)
18 views4 pages

Wow Amazing

1. The document describes code for a server-client model of communication using sockets in Java. It includes code for a ServerSocket class that waits for connection on port 2004, and a Socket class for the client that requests a connection to the server. 2. Both classes create input and output streams to send and receive data between the client and server once connected. They communicate by sending string messages back and forth until the client sends the message "bye" to end the connection. 3. A second part of the document shows code for a chat application using multicast sockets in Java. A server listens on port 1234 for multicast datagrams from clients. Clients can send messages to the group address which are received

Uploaded by

FauziHidayat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Wow Amazing

1. The document describes code for a server-client model of communication using sockets in Java. It includes code for a ServerSocket class that waits for connection on port 2004, and a Socket class for the client that requests a connection to the server. 2. Both classes create input and output streams to send and receive data between the client and server once connected. They communicate by sending string messages back and forth until the client sends the message "bye" to end the connection. 3. A second part of the document shows code for a chat application using multicast sockets in Java. A server listens on port 1234 for multicast datagrams from clients. Clients can send messages to the group address which are received

Uploaded by

FauziHidayat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

ServerSocket
import java.io.*;
import java.net.*;
public class Provider{
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Provider(){}
void run()
{
try{
//1. creating a server socket
providerSocket = new ServerSocket(2004, 10);
//2. Wait for connection
System.out.println("Waiting for connection");
connection = providerSocket.accept();
System.out.println("Connection received from " +
connection.getInetAddress().getHostName());
//3. get Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
sendMessage("Connection successful");
//4. The two parts communicate via the input and output streams
do{
try{
message = (String)in.readObject();
System.out.println("client>" + message);
if (message.equals("bye"))
sendMessage("bye");
}
catch(ClassNotFoundException classnot){
System.err.println("Data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Provider server = new Provider();
while(true){
server.run();
}
}
}

2. Socket
import java.io.*;
import java.net.*;
public class Requester{
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Requester(){}
void run()
{
try{
//1. creating a socket to connect to the server
requestSocket = new Socket("localhost", 2004);
System.out.println("Connected to localhost in port 2004");
//2. get Input and Output streams
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());
//3: Communicating with the server
do{
try{
message = (String)in.readObject();
System.out.println("server>" + message);
sendMessage("Hi my server");
message = "bye";
sendMessage(message);
}
catch(ClassNotFoundException classNot){
System.err.println("data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("client>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Requester client = new Requester();
client.run();
}
}

3. Multicastsocket

import java.net.*;
public class ChatServer {
public static void main(String args[]) throws Exception {
MulticastSocket server = new MulticastSocket(1234);
InetAddress group = InetAddress.getByName("234.5.6.7");
//getByName – Mengembalikan alamat IP yang diberikan oleh Host
server.joinGroup(group);
boolean infinite = true;
/* Server terus-menerus menerima data dan mencetak mereka */
while(infinite) {
byte buf[] = new byte[1024];
DatagramPacket data = new DatagramPacket(buf,
buf.length);
server.receive(data);
String msg = new String(data.getData()).trim();
System.out.println(msg);
}
server.close();
}
}
Berikut adalah class client
import java.net.*;
import java.io.*;
public class ChatClient {
public static void main(String args[]) throws Exception {
MulticastSocket chat = new MulticastSocket(1234);
Pengenalan Pemrograman 2 8
J.E.N.I.
InetAddress group = InetAddress.getByName("234.5.6.7");
chat.joinGroup(group);
String msg = "";
System.out.println("Type a message for the server:");
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
msg = br.readLine();
DatagramPacket data = new DatagramPacket(msg.getBytes(),
0, msg.length(), group, 1234);
chat.send(data);
chat.close();
}
}

You might also like