Click here to Skip to main content
15,920,801 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am Writing a client server program in java with my client having GUI, i want to send username, email id and password to server as soon as connection is established. I am Able to send email and username but i dont know why password is not going to user side. Please hepl me resolving this please find my code here
Java
CLIENT SIDE

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

import java.util.*;
import java.io.*;
public class Clientside extends JFrame implements ActionListener {
	 
	 
	private JLabel user,mail;
	private JTextField usernameinput,emailinput;
	private JPanel username,email,buttonp;
	private JButton connect,terminate;
	private JTextArea result;
	private static Socket socket=null;
	public static void main(String[] args) {
		Clientside Frame = new Clientside();
		Frame.setSize(600, 600);
		Frame.setVisible(true);
		Frame.addWindowListener(new WindowAdapter()
				{
					public void windowClosing(WindowEvent event)
					{
						//Check whether a socket is open...
						if (socket != null)
						{
						try
						{
						socket.close();
						}
						catch (IOException ioEx)
						{
						System.out.println(
						"\nUnable to close link!\n");
						System.exit(1);
						}
						}
						System.exit(0);
						}
				}
				);
		
		
		// TODO Auto-generated method stub

	}
	
	public Clientside()
	{
		username= new JPanel();
		user = new JLabel("username");
		usernameinput= new JTextField("",18);
		username.add(user);
		username.add(usernameinput);
		add(username,BorderLayout.NORTH);
		mail= new JLabel("mail");
		emailinput=new JTextField("",18);
		username.add(mail);
		username.add(emailinput);
		add(username,BorderLayout.NORTH);
	buttonp= new JPanel();
	connect= new JButton("Connect To Server");
	connect.addActionListener(this);
	buttonp.add(connect);
	terminate=new JButton("Terminate the connection");
	terminate.addActionListener(this);
	buttonp.add(terminate);
	add(buttonp,BorderLayout.SOUTH);
	result= new JTextArea(10,10);
	add(result,BorderLayout.CENTER);
	
	
	}
   public void actionPerformed(ActionEvent event)
   {
	   if(event.getSource()==terminate)
	   {
		   System.exit(0);
		   result.setText("");
	   }
	 
		  
		   if(event.getSource()==connect)
			  
		   {
			   String ALPHA_NUM =  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+";
				  StringBuffer sb = new StringBuffer(6);
				  for (int i=0;  i<6;  i++) {  
				         int ndx = (int)(Math.random()*ALPHA_NUM.length());  
				         sb.append(ALPHA_NUM.charAt(ndx));  
				      }  
				  String Password= sb.toString();
			  
			   int port= 2828;
			   String address="127.0.0.1";
			   try{
				
				   InetAddress ipAddress= InetAddress.getByName(address);
				   result.append("Any one with ip"+ ipAddress +"port no. "+ port);
				   
				   
				   socket= new Socket(ipAddress,port);
				  
				  BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			
				  String s=usernameinput.getText();
				bw.write(s);
		
			 bw.flush();
				  String s1=emailinput.getText();
				  bw.write(s1+"\n");
				bw.flush();
				bw.write(Password);
				bw.flush();
				 
			
			
				
				  
				  
				  socket.close();
				   
				 
					   }
			      catch(UnknownHostException uhEx)
			   {
				   result.append("No Host find ");
				   
			   }
			   catch(IOException Ioe)
			   {
				   result.append(Ioe.toString()+"\n");
			   }
			   finally{
				   if(socket!=null)
					   try{
						   
					
					   socket.close();
					   }catch (IOException ioe)
					   {
						   System.out.println("unable to disconnect");
						   System.exit(1);
					   }
				   
					   

			   }
			   
			   
			   }
		   }
}
			   
		   
			   
	   
	   
   

SERVER SIDE


import java.io.*;
import java.net.*;
import java.util.*;
public class Severside {
	
	
	public static void main(String[] args) {
		
		ServerSocket server;
		final int port=2828;
		Socket socket;
		try{
			String j;
			server= new ServerSocket(port);
			do
			{
				socket= server.accept();
			;
				BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
				   String b=br.readLine();
				  
				System.out.println(b);
				
				
				socket.close();
				
				
			}while(true);
			
		}
		catch(IOException IOEX)
		{
			System.out.println(IOEX);
			
		}
	

	}

}


OUTPUT - xyz abc@yahoomail.com
Posted
Updated 30-Jan-12 22:25pm
v2
Comments
Herman<T>.Instance 31-Jan-12 4:25am    
set correct pre tags
vikasdogra 31-Jan-12 4:27am    
my entire code is here i am not getting you digimanus
OriginalGriff 31-Jan-12 4:41am    
He means that he has edited your post and added the correct tags around your code dump to preserve the formatting.

BTW: That is a code dump - don't just chuck your entire program at us, select the relevant code fragments so we don't have to wade through a pile of uncommented code to work out what is significant, and what isn't. Basically, help us to help you!
vikasdogra 31-Jan-12 4:53am    
Sorry i am new to codeproject
OriginalGriff 31-Jan-12 4:56am    
It's cool - just think about it from our side! We need information, but if we have to hunt for it, it can put people off trying.
Use the "Improve question" widget to edit your question and provide better information.

1 solution

Try reading it.
You write the userid and email to the buffer then a new line followed by the password.
On the server you read a single line.
Either write a single line, or read both lines.

Simples.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900