Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Client-Server programm creation

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Client-Server programm creation

    Hi.
    Im creating a client-server programm, in which the server has to only send a requested file by the client. Im having problems on specifying the file.. i tried, but had no luck. The server can handle multiple connections (or at least should). As fas as i have understood, i should make the client send the file name of interest of the server, and then, the server should send the file. The client has a GUI, its code is here too.
    How does everything look overall?
    The server part.

    MyServer code:
    import java.net.*;
    import java.io.*;
     
    class SendData implements Runnable
    {
    	String location="studenti02.txt";
    	Socket newCon;
    	MyClient client;
     
    	SendData(Socket newCon)
    	{
    		this.newCon=newCon;
    		System.out.println("Thread started!");
    		run();  
     
    	}
     
    	public void run()
    	{
    		try
    		{
    			System.out.println("Using port: "+newCon.getPort()); 
    			System.out.println("Adress: "+ newCon.getInetAddress().getHostAddress());
     
    			File file=new File(location);
    			byte [] bytearray=new byte[(int)file.length()];
    			FileInputStream fileInput = new FileInputStream(file);
    			BufferedInputStream bufferedIn=new BufferedInputStream(fileInput);
    			bufferedIn.read(bytearray,0,bytearray.length);
    			OutputStream out= newCon.getOutputStream();
    			out.write(bytearray,0,bytearray.length);
    			out.flush();
    			out.close();
    			bufferedIn.close();
    			newCon.close();
     
    		}
    		catch(IOException e)
    		{
    			System.out.println(e.getStackTrace());
     
    		}
    	}
     
    }
     
    public class MyServer {
    	int port;
    	String location;
    	ServerSocket socket=null;
     
    	MyServer(int port)
    	{
    		this.port=port;
     
    		System.out.println("Server started");
    		try
    		{
    			socket=new ServerSocket(port);
    		}
    		catch(IOException e)
    		{
    			System.out.println(e.getStackTrace());
    		}
     
    		while(true)
    		{
    			try
    			{
    				Socket newCon=socket.accept();
    				SendData conection=new SendData(newCon); 
    			}
    			catch(IOException er)
    			{
    				System.out.println(er.getStackTrace());
    			}
    		}
     
    	}
     
    	public static void main(String[] args) 
    	{
    		MyServer server=new MyServer(1999);
     
    	}
     
    }

    MyClient Code:
    import java.net.*;
    import java.io.*;
     
    public class MyClient {
     
    	ClientFrame window;
    	ErrorMessage err;
     
    	String host;
    	int port;
     
    	int filesize=6022386;
    	int bytesRead;
    	int current=0;
     
    	Socket server;
     
    	MyClient()
    	{	
    		System.out.println("Client started");
    	}
     
    	public void serverCheck()
    	{
    		server.isConnected();
    	}
     
    	public void setHost(String host)
    	{
    		this.host=host;
    	}
     
    	public void setPort(int port)
    	{
    		this.port=port;
    	}
     
    	public void RecieveFile()
    	{
    		try
    		{
    			Socket server=new Socket(host, port);
    			server.setSoTimeout(1000);
     
    			byte [] bytearray=new byte[filesize];
    			InputStream input=server.getInputStream();
     
    			FileOutputStream out=new FileOutputStream("data.txt");
    			BufferedOutputStream  buffer=new BufferedOutputStream(out);
    			bytesRead = input.read(bytearray,0,bytearray.length);
    			current=bytesRead;
     
    			do
    			{
    				bytesRead=input.read(bytearray,current,(bytearray.length-current));
    				if(bytesRead>=0) current+=bytesRead;
    			}while(bytesRead>-1);
     
    			buffer.write(bytearray,0,current);
    			buffer.flush();
    			buffer.close();
    			server.close();
    		}
    		catch(ConnectException b)
    		{
    			err=new ErrorMessage("Could not connect", "Bug found", window);
    			err.setVisible(true);
    		}
    		catch(IOException e)
    		{
    			err=new ErrorMessage("Something is wrong..", "Bug found", window);
    			err.setVisible(true);
    		}
    	}
     
    	public static void main(String[] args) 
    	{
    		MyClient client=new MyClient();
    		ClientFrame frame=new ClientFrame(client);
     
    	}
     
    }

    ClientFrame Code:
    package mess;
     
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
     
    import java.awt.*;
    import java.awt.event.*;
     
    @SuppressWarnings("serial")
     
    public class ClientFrame extends JFrame
    {
    	ErrorMessage dialog;
    	private JTextField host;
    	private JTextField socket;
    	private JTextField file;
    	private JButton button;
     
    	private MyClient client;
     
    	ClientFrame(MyClient client)
    	{
    		this.client=client;
    		StartUI();
    	}
     
    	public void StartUI()
    	{
    		setTitle("Client");
     
    		JPanel textPanel=new JPanel(new GridLayout(3,1));
    		add(textPanel, BorderLayout.CENTER);
    		JPanel info=new JPanel(new GridLayout(3,1));
    		add(info, BorderLayout.WEST);
     
    		host=new JTextField(20);
    		JTextField hostLabel=new JTextField("Host");
    		hostLabel.setEditable(false);
    		hostLabel.setBorder(new EmptyBorder(3,3,3,3));
     
    		socket=new JTextField(20);
    		JTextField socketLabel=new JTextField("Socket");
    		socketLabel.setEditable(false);
    		socketLabel.setBorder(new EmptyBorder(3,3,3,3));
     
    		file=new JTextField(30);
    		JTextField fileLabel=new JTextField("File");
    		fileLabel.setEditable(false);
    		fileLabel.setBorder(new EmptyBorder(3,3,3,3));
     
    		textPanel.add(host);
    		textPanel.add(socket);
    		textPanel.add(file);
     
    		info.add(hostLabel);
    		info.add(socketLabel);
    		info.add(fileLabel);
     
    		button=new JButton("Request");
    		button.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				if(host.getText().equals("") || socket.getText().equals("") || file.getText().equals(""))
    				{
    					dialog=new ErrorMessage("  Something is missing!  ","Excuse me, but..", null);
    					dialog.setVisible(true);
    				}
    				else
    				{
    					try
    					{
    						client.setHost(host.getText());
    						client.setPort(Integer.parseInt(socket.getText()));
    						client.RecieveFile();
    					}
    					catch(NumberFormatException eror)
    					{
    						dialog=new ErrorMessage("Do you know how to use this?","I have to ask..", null);
    						dialog.setVisible(true);
    					}
    				}
    			}
    		});
    		JPanel butPan=new JPanel();
    		add(butPan, BorderLayout.EAST);
    		butPan.add(button, BorderLayout.NORTH);
     
    		setLocationRelativeTo(null);
    		pack();
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
     
    }

    ErrorMessage Code:
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
     
    import java.awt.*;
    import java.awt.event.*;
     
    public class ErrorMessage extends JDialog
    {
    	String message;
    	String windowMes;
    	ClientFrame parent;
    	JTextField errorMes;
     
    	public void Start()
    	{
    		JPanel forMessage=new JPanel();
    		add(forMessage, BorderLayout.CENTER);
    		errorMes=new JTextField();
    		errorMes.setText(message);
    		errorMes.setEditable(false);
    		errorMes.setBorder(new EmptyBorder(10,10,10,10));
    		forMessage.add(errorMes);
     
    		JPanel forButton=new JPanel();
    		add(forButton, BorderLayout.SOUTH);
    		JButton ok=new JButton("Ok");
    		ok.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				setVisible(false);
    			}
    		});
    		forButton.add(ok);
    		pack();
    		setLocationRelativeTo(null);
    	}
     
    	ErrorMessage(String message, String windowMes, ClientFrame parent)
    	{
    		super(parent, windowMes,true);
    		this.message=message;
    		Start();
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Client-Server programm creation

    Im having problems on specifying the file.
    Please explain what the problem is.

    What happens when you execute the code now?

    You need to post code that will compile if you want anyone to test it.
    The posted code is missing import statements and won't compile.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Client-Server programm creation

    Hi.
    Im creating a client-server programm, in which the server has to only send a requested file by the client. Im having problems on specifying the file.. i tried, but had no luck. The server can handle multiple connections (or at least should). As fas as i have understood, i should make the client send the file name of interest of the server, and then, the server should send the file. The client has a GUI, its code is here too.
    How does everything look overall?
    The server part.

    MyServer code:
    import java.net.*;
    import java.io.*;
     
    class SendData implements Runnable
    {
    	String location="studenti02.txt";
    	Socket newCon;
    	MyClient client;
     
    	SendData(Socket newCon)
    	{
    		this.newCon=newCon;
    		System.out.println("Thread started!");
    		run();  
     
    	}
     
    	public void run()
    	{
    		try
    		{
    			System.out.println("Using port: "+newCon.getPort()); 
    			System.out.println("Adress: "+ newCon.getInetAddress().getHostAddress());
     
    			File file=new File(location);
    			byte [] bytearray=new byte[(int)file.length()];
    			FileInputStream fileInput = new FileInputStream(file);
    			BufferedInputStream bufferedIn=new BufferedInputStream(fileInput);
    			bufferedIn.read(bytearray,0,bytearray.length);
    			OutputStream out= newCon.getOutputStream();
    			out.write(bytearray,0,bytearray.length);
    			out.flush();
    			out.close();
    			bufferedIn.close();
    			newCon.close();
     
    		}
    		catch(IOException e)
    		{
    			System.out.println(e.getStackTrace());
     
    		}
    	}
     
    }
     
    public class MyServer {
    	int port;
    	String location;
    	ServerSocket socket=null;
     
    	MyServer(int port)
    	{
    		this.port=port;
     
    		System.out.println("Server started");
    		try
    		{
    			socket=new ServerSocket(port);
    		}
    		catch(IOException e)
    		{
    			System.out.println(e.getStackTrace());
    		}
     
    		while(true)
    		{
    			try
    			{
    				Socket newCon=socket.accept();
    				SendData conection=new SendData(newCon); 
    			}
    			catch(IOException er)
    			{
    				System.out.println(er.getStackTrace());
    			}
    		}
     
    	}
     
    	public static void main(String[] args) 
    	{
    		MyServer server=new MyServer(1999);
     
    	}
     
    }

    MyClient Code:
    import java.net.*;
    import java.io.*;
     
    public class MyClient {
     
    	ClientFrame window;
    	ErrorMessage err;
     
    	String host;
    	int port;
     
    	int filesize=6022386;
    	int bytesRead;
    	int current=0;
     
    	Socket server;
     
    	MyClient()
    	{	
    		System.out.println("Client started");
    	}
     
    	public void serverCheck()
    	{
    		server.isConnected();
    	}
     
    	public void setHost(String host)
    	{
    		this.host=host;
    	}
     
    	public void setPort(int port)
    	{
    		this.port=port;
    	}
     
    	public void RecieveFile()
    	{
    		try
    		{
    			Socket server=new Socket(host, port);
    			server.setSoTimeout(1000);
     
    			byte [] bytearray=new byte[filesize];
    			InputStream input=server.getInputStream();
     
    			FileOutputStream out=new FileOutputStream("data.txt");
    			BufferedOutputStream  buffer=new BufferedOutputStream(out);
    			bytesRead = input.read(bytearray,0,bytearray.length);
    			current=bytesRead;
     
    			do
    			{
    				bytesRead=input.read(bytearray,current,(bytearray.length-current));
    				if(bytesRead>=0) current+=bytesRead;
    			}while(bytesRead>-1);
     
    			buffer.write(bytearray,0,current);
    			buffer.flush();
    			buffer.close();
    			server.close();
    		}
    		catch(ConnectException b)
    		{
    			err=new ErrorMessage("Could not connect", "Bug found", window);
    			err.setVisible(true);
    		}
    		catch(IOException e)
    		{
    			err=new ErrorMessage("Something is wrong..", "Bug found", window);
    			err.setVisible(true);
    		}
    	}
     
    	public static void main(String[] args) 
    	{
    		MyClient client=new MyClient();
    		ClientFrame frame=new ClientFrame(client);
     
    	}
     
    }

    ClientFrame Code:
    package mess;
     
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
     
    import java.awt.*;
    import java.awt.event.*;
     
    @SuppressWarnings("serial")
     
    public class ClientFrame extends JFrame
    {
    	ErrorMessage dialog;
    	private JTextField host;
    	private JTextField socket;
    	private JTextField file;
    	private JButton button;
     
    	private MyClient client;
     
    	ClientFrame(MyClient client)
    	{
    		this.client=client;
    		StartUI();
    	}
     
    	public void StartUI()
    	{
    		setTitle("Client");
     
    		JPanel textPanel=new JPanel(new GridLayout(3,1));
    		add(textPanel, BorderLayout.CENTER);
    		JPanel info=new JPanel(new GridLayout(3,1));
    		add(info, BorderLayout.WEST);
     
    		host=new JTextField(20);
    		JTextField hostLabel=new JTextField("Host");
    		hostLabel.setEditable(false);
    		hostLabel.setBorder(new EmptyBorder(3,3,3,3));
     
    		socket=new JTextField(20);
    		JTextField socketLabel=new JTextField("Socket");
    		socketLabel.setEditable(false);
    		socketLabel.setBorder(new EmptyBorder(3,3,3,3));
     
    		file=new JTextField(30);
    		JTextField fileLabel=new JTextField("File");
    		fileLabel.setEditable(false);
    		fileLabel.setBorder(new EmptyBorder(3,3,3,3));
     
    		textPanel.add(host);
    		textPanel.add(socket);
    		textPanel.add(file);
     
    		info.add(hostLabel);
    		info.add(socketLabel);
    		info.add(fileLabel);
     
    		button=new JButton("Request");
    		button.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				if(host.getText().equals("") || socket.getText().equals("") || file.getText().equals(""))
    				{
    					dialog=new ErrorMessage("  Something is missing!  ","Excuse me, but..", null);
    					dialog.setVisible(true);
    				}
    				else
    				{
    					try
    					{
    						client.setHost(host.getText());
    						client.setPort(Integer.parseInt(socket.getText()));
    						client.RecieveFile();
    					}
    					catch(NumberFormatException eror)
    					{
    						dialog=new ErrorMessage("Do you know how to use this?","I have to ask..", null);
    						dialog.setVisible(true);
    					}
    				}
    			}
    		});
    		JPanel butPan=new JPanel();
    		add(butPan, BorderLayout.EAST);
    		butPan.add(button, BorderLayout.NORTH);
     
    		setLocationRelativeTo(null);
    		pack();
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
     
    }

    ErrorMessage Code:
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
     
    import java.awt.*;
    import java.awt.event.*;
     
    public class ErrorMessage extends JDialog
    {
    	String message;
    	String windowMes;
    	ClientFrame parent;
    	JTextField errorMes;
     
    	public void Start()
    	{
    		JPanel forMessage=new JPanel();
    		add(forMessage, BorderLayout.CENTER);
    		errorMes=new JTextField();
    		errorMes.setText(message);
    		errorMes.setEditable(false);
    		errorMes.setBorder(new EmptyBorder(10,10,10,10));
    		forMessage.add(errorMes);
     
    		JPanel forButton=new JPanel();
    		add(forButton, BorderLayout.SOUTH);
    		JButton ok=new JButton("Ok");
    		ok.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				setVisible(false);
    			}
    		});
    		forButton.add(ok);
    		pack();
    		setLocationRelativeTo(null);
    	}
     
    	ErrorMessage(String message, String windowMes, ClientFrame parent)
    	{
    		super(parent, windowMes,true);
    		this.message=message;
    		Start();
     
    	}
     
    }

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Client-Server creation FIX

    Sorry about the double thread, i edited the previous and it all just disapeared..
    Im writing a server-client program, the server has to send the file the client requests and im not sure how to make that request. The program works (how well is it written?), but the client recieves a frandom file, havent figured out the mechanism for the request. I tried making two funcktions, one for each side. When the server starts, it waits for a connection, then creates a thread that launches a function, to recieve the filename. The client on the other hand, startp up, you enter the desired server, port and filename, then it connects, afterwards sends the file name, then read the file. This DID NOT WORK, possible because the logic it self was wrong, or i just dont know how to hndle these kinds of situations. The socket was opened with the function, and closed after recieving the file. As far as i have googled, it has to do soemthing with that i have to tell when to stop reading.. I really hope that some one can help me, the deadline was cut by two weeks, so this has to be done by the 14th :\
    The code is below, MyServer:
    package mess;
     
    import java.net.*;
    import java.io.*;
     
    class SendData implements Runnable
    {
    	String location="studenti02.txt";
    	Socket newCon;
    	MyClient client;
     
    	SendData(Socket newCon)
    	{
    		this.newCon=newCon;
    		System.out.println("Thread started!");
    		run();  
     
    	}
     
    	public void run()
    	{
    		try
    		{
    			System.out.println("Using port: "+newCon.getPort()); 
    			System.out.println("Adress: "+ newCon.getInetAddress().getHostAddress());
     
    			File file=new File(location);
    			byte [] bytearray=new byte[(int)file.length()];
    			FileInputStream fileInput = new FileInputStream(file);
    			BufferedInputStream bufferedIn=new BufferedInputStream(fileInput);
    			bufferedIn.read(bytearray,0,bytearray.length);
    			OutputStream out= newCon.getOutputStream();
    			out.write(bytearray,0,bytearray.length);
    			out.flush();
    			out.close();
    			bufferedIn.close();
    			newCon.close();
     
    		}
    		catch(IOException e)
    		{
    			System.out.println(e.getStackTrace());
     
    		}
    	}
     
    }
     
    public class MyServer {
    	int port;
    	String location;
    	ServerSocket socket=null;
     
    	MyServer(int port)
    	{
    		this.port=port;
     
    		System.out.println("Server started");
    		try
    		{
    			socket=new ServerSocket(port);
    		}
    		catch(IOException e)
    		{
    			System.out.println(e.getStackTrace());
    		}
     
    		while(true)
    		{
    			try
    			{
    				Socket newCon=socket.accept();
    				SendData conection=new SendData(newCon); 
    			}
    			catch(IOException er)
    			{
    				System.out.println(er.getStackTrace());
    			}
    		}
     
    	}
     
    	public static void main(String[] args) 
    	{
    		MyServer server=new MyServer(1999);
     
    	}
     
    }
    MyClient:
    package mess;
     
    import java.net.*;
    import java.io.*;
     
    public class MyClient {
     
    	ClientFrame window;
    	ErrorMessage err;
     
    	String host;
    	int port;
     
    	int filesize=6022386;
    	int bytesRead;
    	int current=0;
     
    	Socket server;
     
    	MyClient()
    	{	
    		System.out.println("Client started");
    	}
     
    	public void serverCheck()
    	{
    		server.isConnected();
    	}
     
    	public void setHost(String host)
    	{
    		this.host=host;
    	}
     
    	public void setPort(int port)
    	{
    		this.port=port;
    	}
     
    	public void RecieveFile()
    	{
    		try
    		{
    			Socket server=new Socket(host, port);
    			server.setSoTimeout(1000);
     
    			byte [] bytearray=new byte[filesize];
    			InputStream input=server.getInputStream();
     
    			FileOutputStream out=new FileOutputStream("data.txt");
    			BufferedOutputStream  buffer=new BufferedOutputStream(out);
    			bytesRead = input.read(bytearray,0,bytearray.length);
    			current=bytesRead;
     
    			do
    			{
    				bytesRead=input.read(bytearray,current,(bytearray.length-current));
    				if(bytesRead>=0) current+=bytesRead;
    			}while(bytesRead>-1);
     
    			buffer.write(bytearray,0,current);
    			buffer.flush();
    			buffer.close();
    			server.close();
    		}
    		catch(ConnectException b)
    		{
    			err=new ErrorMessage("Could not connect", "Bug found", window);
    			err.setVisible(true);
    		}
    		catch(IOException e)
    		{
    			err=new ErrorMessage("Something is wrong..", "Bug found", window);
    			err.setVisible(true);
    		}
    	}
     
    	public static void main(String[] args) 
    	{
    		MyClient client=new MyClient();
    		ClientFrame frame=new ClientFrame(client);
     
    	}
     
    }
    ClientFrame:
    package mess;
     
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
     
    import java.awt.*;
    import java.awt.event.*;
     
    @SuppressWarnings("serial")
     
    public class ClientFrame extends JFrame
    {
    	ErrorMessage dialog;
    	private JTextField host;
    	private JTextField socket;
    	private JTextField file;
    	private JButton button;
     
    	private MyClient client;
     
    	ClientFrame(MyClient client)
    	{
    		this.client=client;
    		StartUI();
    	}
     
    	public void StartUI()
    	{
    		setTitle("Client");
     
    		JPanel textPanel=new JPanel(new GridLayout(3,1));
    		add(textPanel, BorderLayout.CENTER);
    		JPanel info=new JPanel(new GridLayout(3,1));
    		add(info, BorderLayout.WEST);
     
    		host=new JTextField(20);
    		JTextField hostLabel=new JTextField("Host");
    		hostLabel.setEditable(false);
    		hostLabel.setBorder(new EmptyBorder(3,3,3,3));
     
    		socket=new JTextField(20);
    		JTextField socketLabel=new JTextField("Socket");
    		socketLabel.setEditable(false);
    		socketLabel.setBorder(new EmptyBorder(3,3,3,3));
     
    		file=new JTextField(30);
    		JTextField fileLabel=new JTextField("File");
    		fileLabel.setEditable(false);
    		fileLabel.setBorder(new EmptyBorder(3,3,3,3));
     
    		textPanel.add(host);
    		textPanel.add(socket);
    		textPanel.add(file);
     
    		info.add(hostLabel);
    		info.add(socketLabel);
    		info.add(fileLabel);
     
    		button=new JButton("Request");
    		button.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				if(host.getText().equals("") || socket.getText().equals("") || file.getText().equals(""))
    				{
    					dialog=new ErrorMessage("  Something is missing!  ","Excuse me, but..", null);
    					dialog.setVisible(true);
    				}
    				else
    				{
    					try
    					{
    						client.setHost(host.getText());
    						client.setPort(Integer.parseInt(socket.getText()));
    						client.RecieveFile();
    					}
    					catch(NumberFormatException eror)
    					{
    						dialog=new ErrorMessage("Do you know how to use this?","I have to ask..", null);
    						dialog.setVisible(true);
    					}
    				}
    			}
    		});
    		JPanel butPan=new JPanel();
    		add(butPan, BorderLayout.EAST);
    		butPan.add(button, BorderLayout.NORTH);
     
    		setLocationRelativeTo(null);
    		pack();
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
     
    }
    ErrorMessage:
    package mess;
     
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
     
    import java.awt.*;
    import java.awt.event.*;
     
    public class ErrorMessage extends JDialog
    {
    	String message;
    	String windowMes;
    	ClientFrame parent;
    	JTextField errorMes;
     
    	public void Start()
    	{
    		JPanel forMessage=new JPanel();
    		add(forMessage, BorderLayout.CENTER);
    		errorMes=new JTextField();
    		errorMes.setText(message);
    		errorMes.setEditable(false);
    		errorMes.setBorder(new EmptyBorder(10,10,10,10));
    		forMessage.add(errorMes);
     
    		JPanel forButton=new JPanel();
    		add(forButton, BorderLayout.SOUTH);
    		JButton ok=new JButton("Ok");
    		ok.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				setVisible(false);
    			}
    		});
    		forButton.add(ok);
    		pack();
    		setLocationRelativeTo(null);
    	}
     
    	ErrorMessage(String message, String windowMes, ClientFrame parent)
    	{
    		super(parent, windowMes,true);
    		this.message=message;
    		Start();
     
    	}
     
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Client-Server programm creation

    The threads have been merged

    the client recieves a frandom file,
    Can you explain what that means? Try testing by sending a small file with a simple content that is easily verified. Say one or two lines with simple text like: "AAAAAAAAAAAAAAAAA"


    For testing the dialog window's textfields should all be preloaded so the user does not have to stop and type anything, but only has to press the button.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Client-Server programm creation

    It means, that it can send a server specified file. The file sends, all is well, tried sending a picture even.
    I have to edit, so that the server send a client specified file, aka i ahev to edit the server variable "location", which now is se to be "studenti02.txt".
    Am asking for ideas on how to be abe to change that variable from the client..

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Client-Server programm creation

    server send a client specified file
    Have the client send the name of the file it wants to the server. The server should read the name sent by the client and test if the file is available. If the file is found, read and send it to the client. If not available, send an error message to the client. The server's message to the client will need a header to say if there is a file being sent or an error message.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Client-Server programm creation

    Thanks, but i had that idea all along, i tried to implement that thought, but with no success. What should be the steps? Shouls i create 2 functions for each side or..? Server starts, listens for incoming conections, recieves conection, starts a thread with that conection...
    Ill post those ill functions a bit later.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Client-Server programm creation

    Server waits for a request.
    Client sends request for a file
    Server sends response: requested file or error message
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Client-Server programm creation

    I just started networking and i am having a bit of a trouble understanding it all + the creepy deadline. getLocation is at the start of the run block for the server, fileRequest() is before the client.RecieveFile() call. Are these functions ok? Im getting a null
    Made 2 functions:
    public void fileRequest(String interestFile)
    	{
    		try
    		{
    			OutputStream serverIn=server.getOutputStream();   //here...
    			BufferedWriter sender=new BufferedWriter(new OutputStreamWriter(serverIn));
    			sender.write(interestFile);
    			sender.close();
    		}
    		catch(IOException io)
    		{
     
    		}
    	}

    public void getLocation()
    	{
    		try
    		{
    			InputStream getName=newCon.getInputStream();
    			DataInputStream getterName=new DataInputStream(getName);
    			location=getterName.readUTF();
    			getterName.close();
    		}
    		catch(IOException io)
    		{
     
    		}
     
    	}

    public void actionPerformed(ActionEvent e)
    			{
    				if(host.getText().equals("") || socket.getText().equals("") || file.getText().equals(""))
    				{
    					dialog=new ErrorMessage("  Something is missing!  ","Excuse me, but..", null);
    					dialog.setVisible(true);
    				}
    				else
    				{
    					try
    					{
    						client.setHost(host.getText());
    						client.setPort(Integer.parseInt(socket.getText()));
    						client.setFile(file.getText());
    						client.fileRequest(client.interestFile);
    						client.RecieveFile();
    					}
    					catch(NumberFormatException eror)
    					{
    						dialog=new ErrorMessage("Do you know how to use this?","I have to ask..", null);
    						dialog.setVisible(true);
    					}
    				}
    			}

    public void run()
    	{
    		try
    		{
    			getLocation();
    			System.out.println("Using port: "+newCon.getPort()); 
    			System.out.println("Adress: "+ newCon.getInetAddress().getHostAddress());
     
    			File file=new File(location);
    			byte [] bytearray=new byte[(int)file.length()];
    			FileInputStream fileInput = new FileInputStream(file);
    			BufferedInputStream bufferedIn=new BufferedInputStream(fileInput);
    			bufferedIn.read(bytearray,0,bytearray.length);
    			OutputStream out= newCon.getOutputStream();
    			out.write(bytearray,0,bytearray.length);
    			out.flush();
    			out.close();
    			bufferedIn.close();
    			newCon.close();
     
    		}
    		catch(IOException e)
    		{
    			System.out.println(e.getStackTrace());
     
    		}
    	}

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Client-Server programm creation

    I wouldn't close the connection after sending the request. Wait until the response is received.

    Put calls to the printStackTrace() method in all the catch blocks.

    getLocation() should return a value.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Client-Server programm creation

    How to wait for a response? To wait for a response i should send one, no?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Client-Server programm creation

    Doing a read() would block/wait for a response.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Client-Server programm creation

    Well now, thats does not work. A bit more detail maybe? I bet you can clearly see, that i dont understand completely how these things work yet. Are you collecting posts or something?

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Client-Server programm creation

    thats does not work
    Please explain what "does not work"?
    Can you post the code that shows what you did and explain what happened when it was executed?

    Are you collecting posts or something?
    Can you explain what that means?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Client-Server programm creation

    I already posted the changes, with those two functions, and where they were added. c
    This happens:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at mess.MyClient.fileRequest(MyClient.java:23)
    at mess.ClientFrame$1.actionPerformed(ClientFrame.jav a:77)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Client-Server programm creation

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at mess.MyClient.fileRequest(MyClient.java:23)
    The code at line 23 had a null variable when it was executed. Look at line 23 and find what variable was null and then backtrack in the code to see why that variable did not have a non-null value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: May 31st, 2012, 05:35 PM
  2. [SOLVED] Server/client
    By lorider93p in forum Java Theory & Questions
    Replies: 1
    Last Post: February 8th, 2012, 01:36 PM
  3. server/client application fails when client closes
    By billykid in forum Java Networking
    Replies: 4
    Last Post: January 26th, 2012, 01:54 AM
  4. server and client
    By lanpan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 24th, 2011, 09:29 AM
  5. Apache XML-RPC server creation problem
    By r0x in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 10th, 2011, 09:52 AM