FileName: Chatclient.java
import java.net.*;
import java.io.*;
/**
* <p>title: Network chat bar </p>
* <p>description: This is a client of the chat program using datagram communication </p>
* <p>copyright:copyright (c) 2003</p>
* <p>Filename:ChatClient.java</p>
* @version 1.0
*/
public class chatclient{
private Datagramsocket S;
private inetaddress hostaddress;
private byte[] buf = new byte[1000];
Private Datagrampacket DP = new Datagrampacket (buf,buf.length);
/**
*<br> Method Description: constructor, where the implementation receives user input and communicates with the server
*<br> input Parameters:
*<br> return type:
*/
public chatclient () {
try{
//using the constructor to create a packet socket with any available port on the native
s = new Datagramsocket ();
//Get local IP
hostaddress = inetaddress.getbyname ("localhost");
System.out.println ("Client start .... ...");
while (true) {
String outmessage = "";
//Read input
BufferedReader stdin = new BufferedReader (new InputStreamReader (system.in));
try{
outmessage = Stdin.readline ();
}catch (IOException IE) {
System.err.println ("IO error!");
}
///If input "bye" means exiting the program
if (outmessage.equals ("Bye")) break;
String outstring = "Client say:" + outmessage;
byte[] buf = Outstring.getbytes ();
//packaging data, sending data
Datagrampacket out = new Datagrampacket (buf,buf.length,hostaddress,chatserver.port);
S.send (out);
//Waiting for server to return
S.receive (DP);
String Rcvd = "Rcvd from" + dp.getaddress () + "," + dp.getport () +
":" + New String (Dp.getdata (), 0,dp.getlength ());
System.out.println (RCVD);
}
}catch (unknownhostexception e) {
System.out.println ("Can;t open socket");
system.exit (1);
}catch (socketexception e) {
System.out.println ("Can;t open socket");
E.printstacktrace ();
system.exit (1);
}catch (IOException e) {
System.err.println ("Communication error");
E.printstacktrace ();
system.exit (1);
}catch (Exception e) {
System.err.println ("Communication error");
E.printstacktrace ();
system.exit (1);
}
System.out.println ("ChatClient over");
}
/**
*<br> Method Description: Main method
*<br> input Parameters:
*<br> return type:
*/
public static void Main (string[] args) {
new ChatClient ();
}
}
import java.net.*;
import java.io.*;
import java.util.*;
/**
* <p>title: Network chat bar </p>
* <p>description: Chat servers created using datagrams </p>
* <p>copyright:copyright (c) 2003</p>
* <p>Filename:ChatServer.java</p>
* @version 1.0
*/
public class chatserver{
static final int port = 4000;//Set Service port
private byte[] buf = new byte[1000];
private Datagrampacket DGP =new datagrampacket (buf,buf.length);
Private Datagramsocket SK;
/**
*<br> Method Description: Server-side constructor, to achieve read user input and communication
*<br> input Parameters:
*<br> return type:
*/
public Chatserver () {
try{
//Materialized datagram
SK = new Datagramsocket (PORT);
System.out.println ("Server start ... .... ..."), and the;
while (true) {
//Waiting to receive
sk.receive (DGP);
//Get receive information
String rcvd = new String (Dgp.getdata (), 0,dgp.getlength ()) +
", From Address:" + dgp.getaddress () +
", Port:" + dgp.getport ();
System.out.println (RCVD);
String outmessage = "";
//Read input
BufferedReader stdin = new BufferedReader (new InputStreamReader (system.in));
try{
outmessage = Stdin.readline ();
}catch (IOException IE) {
System.err.println ("IO error!");
}
String outstring = "Server say:" + outmessage;
//Copy characters to cache
byte[] buf = Outstring.getbytes ();
//package data, send back information.
Datagrampacket out = new Datagrampacket (buf,buf.length,dgp.getaddress (), Dgp.getport ());
Sk.send (out);
}
}catch (socketexception e) {
System.err.println ("Can´t open socket");
system.exit (1);
}catch (IOException e) {
System.err.println ("Communication error");
E.printstacktrace ();
system.exit (1);
}
}
public static void Main (string[] args) {
new Chatserver ();
}
}