dcsimg
Client/server communication
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Arif_Mohammed
Posted On:   Saturday, May 27, 2006 04:20 AM

New Page 2 Hi, i am doing a project based on client / server, in this i made a GUI with add, remove button using hashtable. not database is used in the program. i am unable to send data from client to server. i am giving my code below please help me in getting the solution. CLIENT.java import java.io.*; import java.awt.*; import java.awt.event.*; import java.net.*; import javax.swing.*; import java.util.*; public class Client extends JFrame { private static final int PORT_NUM = 5234; private static   More>>
New Page 2

Hi,
i am doing a project based on client / server, in this i made a GUI with add, remove button using hashtable. not database is used in the program. i am unable to send data from client to server.
i am giving my code below please help me in getting the solution.

CLIENT.java












import java.io.*;

import java.awt.*;

import java.awt.event.*;

import java.net.*;

import javax.swing.*;

import java.util.*;



public class Client extends JFrame

{



private static final int PORT_NUM = 5234;

private static String address;

String username;



JButton remove, list, add, close;

JLabel Member;

JTextField member;

Socket client = null;

ObjectInputStream input = null;

PrintStream output = null;

MyObject my = new MyObject(12);



public Client()

{

super("Client");



//added here

Member = new JLabel("Member");

getContentPane().add(Member);

member = new JTextField(20);

getContentPane().add(member);



//end



remove = new JButton("REMOVE");

list = new JButton("LIST");

add = new JButton("ADD");

close = new JButton("CLOSE");

Container c = getContentPane();



c.add(add);

c.add(remove);

c.add(list);

c.add(close);

c.setLayout(new FlowLayout());



/* */



/* try

{

client=new Socket(InetAddress.getByName("192.9.200.120"),PORT_NUM);

input=new ObjectInputStream(client.getInputStream());

output=new PrintStream(client.getOutputStream());

}

catch(Exception e)

{

System.out.println("Accept Failed: 1001");

System.exit(-1);

}*/

setSize(500, 500);

setVisible(true);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

add.addActionListener(new ActionListener());

close.addActionListener();

list.addActionListener();

remove.addActionListener();

}



public void actionPerformed(ActionEvent e1) {

JButton button = (JButton) e1.getSource();

if (button.equals(close))

{

this.dispose(); //close the current frame

}

else if (button.equals(add)) {

username = member.getText();

my.addMem(username);

}

else if (button.equals(remove)) {

username = member.getText();

my.removeMember(Integer.parseInt(username));

}

else if (button.equals(list))

{

Enumeration e=my.getAllMembers();

}

}



public void runClient()

{

try {

client = new Socket(InetAddress.getByName("192.9.200.120"), PORT_NUM);



output.println("Created Socket
");

input = new ObjectInputStream(client.getInputStream());

//client.close();

}

catch (IOException e) {

e.printStackTrace();

}

}



public static void main(String args[]) {

if (args.length != 1) {

System.out.println("
Usage: java Client server_address");

System.exit(0);

}

Client c = new Client();

c.address = args[0];

c.runClient();

}

}



 








ClubMember.java







New Page 3




public class ClubMember {



// Instance Variables

private String memName;

private int memNumber;



// Static class Variable

public static int membershipNumber=0;



//Constructor

ClubMember(String nam){

memName = nam;

memNumber = membershipNumber++;

}



// Methods

public String getName(){

return memName;

}



public int getNumber(){

return memNumber;

}





public String toString(){

return ("ClubMember: Name=" + memName + " Number=" + memNumber);

}



}







 







MyObject.java

import java.io.*;
import java.util.*;
public class MyObject implements Serializable
{
private String name;
private int number,max;
private Hashtable members;
public MyObject(int size)
{
members=new Hashtable();
max=size;
}
public void addMem(String name)
{
if(getNumbMembers()>=getMaxNumbMembers())
{
ClubMember newone = new ClubMember(name);
members.put(new Integer(newone.getNumber()), newone);
}
}

public Enumeration getAllMembers()
{
return members.elements();
}

public void removeMember( int number )
{
members.remove(new Integer(number));
}

public int getNumbMembers()
{
return members.size();
}

public int getMaxNumbMembers()
{
return max;
}


}





Server.java

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class Server extends Frame
{
private static final int PORT_NUM=5234;
private static final int Q_LENGTH=5;
TextArea display;
ServerSocket server;
Socket connection;
OutputStream output;
Thread serverThread;

public Server()
{
super("Server");
display = new TextArea(20,5);
add("Center", display);
setSize(300,150);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
try
{
server=new ServerSocket(PORT_NUM, Q_LENGTH);
serverThread=new Thread();
serverThread.start();
display.setText("CONNECTION SUCCESSFUL");
display.append("Sending Message:
. CONNECTION SUCCESSFUL
");
//output=connection.getOutputStream();
String s= new String("CONNECTION SUCCESSFUL
");
for(int i=0;i
output.write((int)s.charAt(i));
// display.append("Transmission Complete. Closing socket.
");
// connection.close();
}
catch(IOException e)
{
e.printStackTrace();
}

}
/* public void runServer()
{
try
{
server=new ServerSocket(PORT_NUM, Q_LENGTH);
connection=server.accept();
display.setText("CONNECTION SUCCESSFUL");
display.append("Sending Message:
. CONNECTION SUCCESSFUL
");
output=connection.getOutputStream();
String s= new String("CONNECTION SUCCESSFUL
");
for(int i=0;i
output.write((int)s.charAt(i));
// display.append("Transmission Complete. Closing socket.
");
// connection.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}*/
public static void main(String args[])
{
new Server();
//s.runServer();
}
public void run()
{
try{
while(true)
{
connection=server.accept();
}
}catch(Exception exe)
{
System.out.println("can't listen to the client"+exe);
}

}
}    <<Less

Re: Client/server communication

Posted By:   Robert_Lybarger  
Posted On:   Saturday, May 27, 2006 09:46 AM

Lets quickly review my personal recommendations for asking a question on a forum: (1) reduce your code to the minimum amount of code required to illustrate your problem. Posting all four classes at about a hundred lines each won't get me interested enough to want to copy/paste it all, must less compile/run/debug it. A hundred lines total is probably pushing the upper boundary. (2) actually try to explain the expected behavior (click on 'XYZ', then type in 'blah', then it is supposed to ...). (3) explain the behavior you're actually getting. (4) use preview mode to be sure that *all* your code is properly formatted (indented). (Consider how much more readable this would be if I used formatted HTML, for example.)
About | Sitemap | Contact