Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 22nd, 2006, 12:11 AM
Registered User
Join Date: Jan 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with socket connection

i have my gps reciver connected to the usb port - i have a daemon gpsd running which makes data available on tcp port 2947 for querying. when i do telnet, it gives proper data.
but when i open a socket connection using java, it does not print anything as output. actually telnet asks for an escape charatcer so i am sending "r" initially to the server but still the program does not print anything as output.

here is my java code -
import java.io.*;
import java.net.Socket;

public class test2
public static void main(String[] args)
Socket s = new Socket("localhost",2947);

PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(

BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;

line = in.readLine();
catch (Exception e)

or sometimes it even shows error as
Exception in thread "main" java.net.SocketException: Invalid argument or cannot assign requested address
at java.net.PlainSocketImpl.socketConnect(Native Method)

and this is the output which i get on telnet -
ot@localhost ~]# telnet localhost 2947
Connected to localhost.
Escape character is '^]'.
$GPRMC,000212,V,18000.0000,N,00000.0000,W,0.0000,1 80.000,101102,,*1A
$GPRMC,000213,V,18000.0000,N,00000.0000,W,0.0000,1 80.000,101102,,*1B
$GPRMC,000214,V,18000.0000,N,00000.0000,W,0.0000,1 80.000,101102,,*1C

if you can figure out the problem, please let me know

Old January 23rd, 2006, 01:36 PM
Friend of Wrox
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts

Just a guess but try import java.net.*; as well as importing java.net.socket - it looks like it's having problems converting localhost into an IP (it's a while since I did any java socket stuff).

Don't Stand on your head - you'll get footprints in your hair

Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket connection Lizane Java GUI 1 September 23rd, 2007 11:11 PM
how to create a socket connection? Janet Classic ASP Professional 0 December 30th, 2003 06:42 PM
Secure Socket Connection planeswalk ASP.NET 1.0 and 1.1 Professional 0 July 22nd, 2003 11:04 PM
Secure Socket Connection planeswalk Pro VB.NET 2002/2003 0 July 22nd, 2003 08:24 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.