Results 1 to 2 of 2
Thread: UnknownHostException?
- 07-31-2007, 09:13 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 1
- Rep Power
- 0
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:
Below is the client 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); } } }
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.
- 08-02-2007, 04:15 PM #2
Member
- Join Date
- Aug 2007
- Posts
- 47
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks