View Single Post
  #1 (permalink)  
Old 07-31-2007, 11:13 AM
gautam89 gautam89 is offline
Member
 
Join Date: Jul 2007
Posts: 1
gautam89 is on a distinguished road
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:

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:

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.
Reply With Quote
Sponsored Links