Java Socket – Client and Server Communication


/*  UDP – User Data Protocol – Client and server communication

A Graphical User Interface Program */

 

import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class GUIClient extends JFrame
{

JButton send=new JButton(“Send”);
JTextField msg=new JTextField(20);
TextArea chat=new TextArea(20,20);
DatagramSocket ds=null;
DatagramPacket dp=null;

public class Receiver extends Thread
{
Thread t=null;
public Receiver()
{
t=new Thread(this);
t.start();
}
public void run()
{
String str1=”Server:”;
String str2;
byte data[]=new byte[1024];
try{
do{
DatagramPacket dp=new DatagramPacket(data,1024);
ds.receive(dp);
str2=dp.getData().toString();
chat.append(str1+str2+”\n”);

}while(!str2.equals(“end”));
}catch(Exception e)
{System.out.println(e);}
}
}

public static void main(String s[])
{
JFrame obj=new GUIClient();
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.setTitle(“CLIENT”);
obj.setVisible(true);
obj.setSize(400,400);
}

public GUIClient()
{
try{
ds=new DatagramSocket(7090);
}
catch(Exception e)
{
System.out.println(e);
}
components();
events();
Receiver r=new Receiver();
}

void components()
{
getContentPane().setLayout(null);
send.setBounds(270,20,80,30);
msg.setBounds(50,20,200,20);
chat.setBounds(20,60,320,300);
getContentPane().add(send);
getContentPane().add(msg);
getContentPane().add(chat);
}

void events()
{
send.addActionListener(new s());
}

public class s implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
String str3;
byte data2[]=new byte[1024];
try{
InetAddress ia=InetAddress.getByName(“localhost”);// or IP of server
str3=msg.getText();
data2=str3.getBytes();
dp=new DatagramPacket(data2,data2.length,ia,7000);
ds.send(dp);
//ds.close();
}catch(Exception e1)
{
System.out.println(e1);
}
}
}
}

—————————–

import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class GUIServer extends JFrame
{

JButton send=new JButton(“Send”);
JTextField msg=new JTextField(20);
TextArea chat=new TextArea(20,20);
DatagramSocket ds=null;
DatagramPacket dp=null;

public class Receiver extends Thread
{
Thread t=null;
public Receiver()
{
t=new Thread(this);
t.start();
}
public void run()
{
String str1=”Client:”;
String str2;
byte data[]=new byte[1024];
try{
do{
DatagramPacket dp=new DatagramPacket(data,1024);
ds.receive(dp);
str2=dp.getData().toString();
chat.append(str1+str2+”\n”);
}while(!str2.equals(“end”));
}catch(Exception e)
{System.out.println(e);}
}
}

public static void main(String s[])
{
JFrame obj=new GUIServer();
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.setTitle(“SERVER”);
obj.setVisible(true);
obj.setSize(400,400);
}

public GUIServer()
{
try{
ds=new DatagramSocket(7000);
}
catch(Exception e)
{
System.out.println(e);
}
components();
events();
Receiver r=new Receiver();
}

void components()
{
getContentPane().setLayout(null);
send.setBounds(270,20,80,30);
msg.setBounds(50,20,200,20);
chat.setBounds(20,60,320,300);
getContentPane().add(send);
getContentPane().add(msg);
getContentPane().add(chat);
}

void events()
{
send.addActionListener(new s());
}

public class s implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
String str3;
byte data2[]=new byte[1024];
try{
InetAddress ia=InetAddress.getByName(“localhost”);
str3=msg.getText();
data2=str3.getBytes();
dp=new DatagramPacket(data2,data2.length,ia,7090);
ds.send(dp);
//ds.close();
}catch(Exception e1)
{
System.out.println(e1);
}
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s