Results 1 to 2 of 2
  1. #1
    gautam89 is offline Member
    Join Date
    Jul 2007
    Posts
    1
    Rep Power
    0

    Default UnknownHostException?

    Hi,

    I'm sorry if this sort of question has been answered before. I've looked through many of the threads pertaining to sockets, but was not able to find where I was going wrong.

    I need to create a program that can communicate with another PC on the same LAN network for my computer science project. When I run the client program I get the following error:

    Exception in thread "main"
    java.net.UnknownHostException: GAUTAM-LAPTOP
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at tcpclient.main(tcpclient.java:12)


    I'm running the server program on a laptop with name GAUTAM-LAPTOP. I've configured my router to direct port 1234 (TCP) to GAUTAM-LAPTOP as well.

    Below is the server code I am using:

    Java Code:
    import	java.io.*;
    import	java.net.*;
    
    public class tcpserver
    {
    	public static void main(String args[]) throws Exception
    	{
    		String	message;
    		String	messageout;
    		ServerSocket ssock = new ServerSocket(1234);
    		while(true)
    		{
    			Socket	connsock = ssock.accept();
    			InputStreamReader instr =  
    				new InputStreamReader(connsock.getInputStream());
    			DataOutputStream outstr = 
    				new DataOutputStream(connsock.getOutputStream());
    			BufferedReader innet = new BufferedReader(instr);
    			message = innet.readLine();
    			messageout = message.toUpperCase() + "\n";
    			outstr.writeBytes(messageout);
    		}
    	}
    }
    Below is the client code I am using:

    Java Code:
    import	java.io.*;
    import	java.net.*;
    
    public	class	tcpclient	
    {
    	public	static	void	main(String[] args) throws Exception
    	{
    		String	message;
    		String	modifiedmessage;
    		BufferedReader inkbd = 
    			new BufferedReader(new InputStreamReader(System.in));
    		Socket	csock = new Socket("GAUTAM-LAPTOP",1234);
    		DataOutputStream out = 
    			new DataOutputStream(csock.getOutputStream());
    		BufferedReader innet = 
    			new BufferedReader(new InputStreamReader(csock.getInputStream()));
    		message = inkbd.readLine();
    		out.writeBytes(message + "\n");
    		modifiedmessage = innet.readLine();
    		System.out.println("Server sent :- " + modifiedmessage);
    		csock.close();
    	}
    }

    Can someone please give me some guidance as to what I am doing wrong and why the error is coming? Also, is there an easier way to implement this - I only need it for communication between computers on the same network.

  2. #2
    henry_78 is offline Member
    Join Date
    Aug 2007
    Posts
    47
    Rep Power
    0

    Default

    I think u have a problem with translating your lap-top name in an IP address..try to modify your hosts file adding an entry with your lap-top name and ip

Posting Permissions

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