Send user defined object to the server

package network1;
import java.io.*;
class Message implements Serializable
{
    String custName;
    String concernPerson;
    String custMessage;
}

Download

import javax.swing.*;
import java.net.*;
import java.io.*;
import java.awt.TextArea;
import java.awt.event.*;
public class Client1 implements ActionListener {
	JFrame f1;
	JPanel p1;
	JLabel l1, l2, l3;
	JTextField t1;
	TextArea ta;
	JList list;
	String str[] = { "Sales Person", "Branch Manager", "head", "Corporate head" };
	JButton b1, b2;
	public Client1() {
		f1 = new JFrame("My window");
		f1.setSize(400, 500);
		f1.add(p1 = new JPanel());
		p1.add(l1 = new JLabel("Enter Customer name/Acc No"));
		p1.add(t1 = new JTextField(20));
		p1.add(l2 = new JLabel("select Concern person"));
		p1.add(list = new JList(str));
		p1.add(l3 = new JLabel("Enter message "));
		p1.add(ta = new TextArea(10, 20));
		p1.add(b1 = new JButton("Save"));
		p1.add(b2 = new JButton("reset"));
		b1.addActionListener(this);
		b2.addActionListener(this);
		f1.setVisible(true);
	}

	public void actionPerformed(ActionEvent evt) {
		if (evt.getSource() == b1) {
			Message msg = new Message();
			msg.custName = t1.getText();
			msg.concernPerson = list.getSelectedValue().toString();
			msg.custMessage = ta.getText();
			try {
				Socket toServer = new Socket("localhost", 505);
				ObjectOutputStream streamToServer = new ObjectOutputStream(
						toServer.getOutputStream());
				streamToServer.writeObject((Message) msg);
				BufferedReader bf = new BufferedReader(new InputStreamReader(
						toServer.getInputStream()));
				String messageFromServer = bf.readLine();
				if (messageFromServer.length() != 0) {
					javax.swing.JOptionPane.showMessageDialog(null,
							messageFromServer, "from server", 1);
					streamToServer.close();
					bf.close();
				}
			} catch (Exception ex) {
				javax.swing.JOptionPane.showMessageDialog(null,
						"message sending error \n" + ex,
						"message senind error", 1);
			}
		}
		if (evt.getSource() == b2) {		}
	}
	public static void main(String[] args) {
		new Client1();
	}
}

Download

package network1;
import java.net.*;
import java.io.*;
public class Server1 extends Thread {
	ServerSocket serverSocket;
	Socket netClient;
	ObjectInputStream fromClient;
	PrintStream toClient;
	public Server1() {
		try {
			serverSocket = new ServerSocket(505);
		} catch (Exception e) {
			System.out.println("counld not create server" + e);
		}
		System.out.println("Server started !!!!!!!");
		this.start();
	}
	public void run() {
		try {
			while (true) {
				Socket socketClient = serverSocket.accept();
				displayDetails(socketClient);
			}
		} catch (Exception e) {
			System.out.println("server not listen " + e);
		}
	}
	public static void main(String args[]) {
		new Server1();
	}
	public void displayDetails(Socket client) {
		netClient = client;
		Message msg;
		try {
			fromClient = new ObjectInputStream(netClient.getInputStream());
			toClient = new PrintStream(netClient.getOutputStream());
			msg = (Message) fromClient.readObject();
			if (msg == null)
				return;
			// Send acknowledgement to the client
			RandomAccessFile fobj = new RandomAccessFile("complain.txt", "rw");
			fobj.seek(fobj.length());
			fobj.writeBytes(msg.custName + "    " + msg.concernPerson + "    "
					+ msg.custMessage + "\n");
			System.out.println(msg.custName + "    " + msg.concernPerson
					+ "    " + msg.custMessage);
			toClient.println(msg.custName
					+ "   your complain is registered \n thanks ");
			fobj.close();
		} catch (Exception e) {
			try {
				netClient.close();
			} catch (IOException e1) {
				System.err.println("Unable to set up streams" + e1);
				return;
			}
		}
	}
}

Download

Network Programming in Java

InetAddress Demo
Server/Client with Single threaded and multi threaded Server
Server/Client with Single threaded and multi threaded Server


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code