Results 1 to 1 of 1
  1. #1
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Client-Server creation

    Hi.
    I have a mighty task at hand, mostly because the deadline was trimmed by two weeks, so im in need of some advice. I have to create a client-server programm, which does the following:
    The server must handle multiple conections (atleast three). The servers has to send a file to the client, the client manipulates it and shows the results of the file. Each have to show a lot of information during those proceses, error handling, files that dont have all the information needed. To write the whole task it would be a hassle to translate, need that time for other purposes. So im thinking how to make this happen. I have written a base code, which should handle multiple connections, is this a ok way to do it? Its nothing advanced, i just started networking today..
    Any help, advice would be gratly appriciated!

    Forgot one thing, when using readUTF(), it reads nothing or throws an error, what could be the possible reasons?
    The server:
    Java Code:
    import java.net.*;
    import java.io.*;
    
    class SendData implements Runnable
    {
    	Socket newCon;
    	SendData(Socket newCon)
    	{
    		this.newCon=newCon;
    		
    		System.out.println("Thread started!");
    		run(); 
    	}
    	
    	public void run()
    	{
    		try
    		{
    			OutputStream socketOut=newCon.getOutputStream();
    			BufferedWriter socketIn=new BufferedWriter(new OutputStreamWriter(socketOut));
    			socketIn.write("Hello, who are you?");
    			socketIn.close();
    			newCon.close();
    		}
    		catch(IOException e)
    		{
    			System.out.println(e.getStackTrace());
    		}
    	}
    }
    
    public class MyServer {
    	int port;
    	int active;
    	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(19999);
    
    	}
    
    }
    The Client:
    Java Code:
    package mess;
    
    import java.net.*;
    import java.io.*;
    
    public class MyClient {
    
    	String host;
    	int port;
    	
    	Socket server;
    	
    	MyClient(String host, int port)
    	{
    		this.host=host;
    		this.port=port;
    		
    		System.out.println("Client started");
    		try
    		{
    			Socket server=new Socket(host, port);
    			InputStream fromServer=server.getInputStream(); 
    			DataInputStream dataFrom=new DataInputStream(fromServer);
    			System.out.println(dataFrom.readLine());
    			dataFrom.close();
    			server.close();
    		}
    		catch(ConnectException b)
    		{
    			System.out.println("Could not connect!");
    			System.exit(0);
    		}
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    	}
    	
    	public static void main(String[] args) 
    	{
    		MyClient client=new MyClient("192.168.0.100",19999);
    	}
    
    }
    Last edited by MustSeeMelons; 12-10-2012 at 07:11 PM.

Similar Threads

  1. Replies: 3
    Last Post: 06-03-2012, 09:48 AM
  2. Replies: 1
    Last Post: 06-03-2012, 03:07 AM
  3. Replies: 2
    Last Post: 09-24-2010, 11:31 AM
  4. Replies: 1
    Last Post: 10-05-2008, 03:12 PM
  5. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •