Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

java socket connecting to c socket?

235 views
Skip to first unread message

Kay

unread,
Oct 20, 2002, 4:17:42 AM10/20/02
to
Hi, all:

Is it true that java Socket can only connect to java ServerSocket? I have a
binary application using c socket. Is there any way I can write a java
socket client to talk with it?

-Kay

Gordon Beaton

unread,
Oct 20, 2002, 4:18:32 AM10/20/02
to
On Sun, 20 Oct 2002 01:17:42 -0700, Kay wrote:
> Is it true that java Socket can only connect to java ServerSocket?

No. TCP doesn't care what language your application was written in.

> I have a binary application using c socket. Is there any way I can
> write a java socket client to talk with it?

Create a java Socket. Connect to your server.

However depending on how the C application was written, you may have
problems that are due to assumptions about byte order and sizes of
various datatypes, among other things. What does the protocol look
like?

/gordon

--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m

Thomas Urlaub

unread,
Oct 20, 2002, 4:26:34 AM10/20/02
to
Hi,

> Is it true that java Socket can only connect to java ServerSocket?

The class ServerSocket is used to listen for new incoming connections, so if
you are writing an application which acts as a server waiting for new
connections from clients, you would use a ServerSocket (or better
ServerSocketChannel with java 1.4).

A client application uses a Socket (or better SocketChannel with java 1.4)
to connect to a server application...

> I have a binary application using c socket. Is there any way
> I can write a java socket client to talk with it?

Yes, sockets are just the end points of TCP/IP communication. You can write
applications in whatever language you want. The only important thing is:
Your applications have to use the same protocol on their network
connection.

Thomas

Kay

unread,
Oct 20, 2002, 10:09:07 PM10/20/02
to
> However depending on how the C application was written, you may have
> problems that are due to assumptions about byte order and sizes of
> various datatypes, among other things. What does the protocol look
> like?
I can't even new a Socket.
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:295)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:161)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:148)
at java.net.Socket.connect(Socket.java:425)
at java.net.Socket.connect(Socket.java:375)
at java.net.Socket.<init>(Socket.java:290)
at java.net.Socket.<init>(Socket.java:118)

the corresponding c socket client,which works, is as follows:

//- Initialisation - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Windows-specific initialisation.

WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 2, 0 );
if(WSAStartup( wVersionRequested, &wsaData ) != 0)
{
std::cout << "Socket Initialization Error" << std::endl;
return 1;
}

// Create Socket

SOCKET SocketHandle = INVALID_SOCKET;

struct protoent* pProtocolInfoEntry;
char* protocol;
int type;

protocol = "tcp";
type = SOCK_STREAM;

pProtocolInfoEntry = getprotobyname(protocol);
assert(pProtocolInfoEntry);

if(pProtocolInfoEntry)
SocketHandle = socket(PF_INET, type, pProtocolInfoEntry->p_proto);

if(SocketHandle == INVALID_SOCKET)
{
std::cout << "Socket Creation Error" << std::endl;
return 1;
}

// Find Endpoint

if(argc != 2)
{
std::cout << "Usage: " << argv[0] << " <address>" << std::endl;
return 1;
}

struct hostent* pHostInfoEntry;
struct sockaddr_in Endpoint;

static const int port = 800;

memset(&Endpoint, 0, sizeof(Endpoint));
Endpoint.sin_family = AF_INET;
Endpoint.sin_port = htons(port);

pHostInfoEntry = gethostbyname(argv[1]);

if(pHostInfoEntry)
memcpy(&Endpoint.sin_addr, pHostInfoEntry->h_addr,
pHostInfoEntry->h_length);
else
Endpoint.sin_addr.s_addr = inet_addr(argv[1]);

if(Endpoint.sin_addr.s_addr == INADDR_NONE)
{
std::cout << "Bad Address" << std::endl;
return 1;
}

// Create Socket

int result = connect( SocketHandle, (struct sockaddr*) & Endpoint,
sizeof(Endpoint));

if(result == SOCKET_ERROR)
{
std::cout << "Failed to create Socket" << std::endl;
int e = WSAGetLastError();
return 1;
}

my java socket client, which does not work, is:
Socket socket=new Socket("localhost",port);

any idea how to translate the c junk to java to make it match with the c
socket server?

thanks,
-Kay


Jon A. Cruz

unread,
Oct 20, 2002, 10:52:37 PM10/20/02
to
Kay wrote:
> the corresponding c socket client,which works, is as follows:

> WORD wVersionRequested;


> WSADATA wsaData;
> wVersionRequested = MAKEWORD( 2, 0 );
> if(WSAStartup( wVersionRequested, &wsaData ) != 0)


Eeeeeeeeek!!!!


That's not a "c socket", that's a winsock! =-O


Jon A. Cruz

unread,
Oct 20, 2002, 11:21:39 PM10/20/02
to
Kay wrote:
> my java socket client, which does not work, is:
> Socket socket=new Socket("localhost",port);
>
> any idea how to translate the c junk to java to make it match with the c
> socket server?


1) Is your computer using "localhost" for a name? Can you ping it from a
command-line?

2) Are you sure that you are specifying the exact same host name and
port in C and Java?

3) From the command-line, can you do:
telnet localhost 800

?

Kay

unread,
Oct 20, 2002, 11:43:32 PM10/20/02
to
> 1) Is your computer using "localhost" for a name? Can you ping it from a
> command-line?

Yes.

> 2) Are you sure that you are specifying the exact same host name and
> port in C and Java?


Yes. At least this java code works if I use java ServerSocket at the server.

> 3) From the command-line, can you do:
> telnet localhost 800

Could not open a connection to host on port 800 : Connection failed.

-Kay


Kay

unread,
Oct 20, 2002, 11:44:11 PM10/20/02
to
> 1)

Yes.

> 2)

Yes. At least this java code works if I use java ServerSocket at the server.

> 3)

Jon A. Cruz

unread,
Oct 20, 2002, 11:47:11 PM10/20/02
to
Kay wrote:
>>2) Are you sure that you are specifying the exact same host name and
>>port in C and Java?
>
>
>
> Yes. At least this java code works if I use java ServerSocket at the server.

No. You're probably mistaken. See answer 3.

>
>
>>3) From the command-line, can you do:
>>telnet localhost 800
>
>
> Could not open a connection to host on port 800 : Connection failed.

So...

The problem is not with Java. It's with the C server you are using.
(well, that or your network setup, but if a C client can hit that C
server, then the network is not your problem).

Since you can't access that port via telnet, that port is not up and
listening. At least it's not listening where you think it is.

Try dumping all info in your C server untill things work. Telnet can
prerry much be assumed to work, otherwise Microsoft would never have
shipped it. :-)


Chances are either your server is listening on a different port than you
expect, or your server is malfunctioning altogether.

Kay

unread,
Oct 20, 2002, 11:59:47 PM10/20/02
to
> Since you can't access that port via telnet, that port is not up and
> listening. At least it's not listening where you think it is.
D:\>netstat -a
...
TCP kay:800 kay:0 LISTENING
...
I think that's just my protocol does not match that of telnet. Since the
protocol is defined to transfer some data to our own needs.

Don't you think that's something that winsock does while java sockets don't?

thanks,
-Kay

Kay

unread,
Oct 21, 2002, 12:46:08 AM10/21/02
to
wow, problem solved, after I substitute localhost with my own machine's ip.
I thought localhost is the same as my own ip, but....

thanks for all your help.

-Kay


Gordon Beaton

unread,
Oct 21, 2002, 2:21:19 AM10/21/02
to
On Sun, 20 Oct 2002 21:46:08 -0700, Kay wrote:
> wow, problem solved, after I substitute localhost with my own
> machine's ip. I thought localhost is the same as my own ip, but....

This problem is because you are binding the (server) socket to the
hosts IP address. If you specify INADDR_ANY instead, then clients can
connect via either interface, i.e. localhost or the "real" host
address.

Kay

unread,
Oct 21, 2002, 4:01:40 AM10/21/02
to
I hope those people who wrote the server know this. I only have the binary,
can't really do anything.

or ask them to use java.;-)

Jon A. Cruz

unread,
Oct 21, 2002, 12:20:05 PM10/21/02
to Kay
Kay wrote:
> D:\>netstat -a
> ...
> TCP kay:800 kay:0 LISTENING
> ...
> I think that's just my protocol does not match that of telnet. Since the
> protocol is defined to transfer some data to our own needs.

Protocol's have nothing to do with "match"ing telnet.

Connecting to a socket is like making a telephone call. You dial the
number and hope someone picks it up. You might either get a busy signal,
a disconnect notice or just nobody picking it up.

However, once someone does pick up the phone, then you're onto your
protocol. The person might not speak the same language you do, but that
has nothing to do with establishing the call itself.


Oh, and your actual problem of using the wrong hostname to try to
connect one could liken to using the wrong area code when dialing a
long-distance number. :-)

Kay

unread,
Oct 21, 2002, 11:59:46 PM10/21/02
to
> However depending on how the C application was written, you may have
> problems that are due to assumptions about byte order and sizes of
> various datatypes, among other things.

Any easy way to stop Java from converting the byte order?

-Kay


Kay

unread,
Oct 22, 2002, 12:44:35 AM10/22/02
to

Jon A. Cruz

unread,
Oct 22, 2002, 1:35:52 AM10/22/02
to


But be sure that the C server is doing the right thing.

Rainman

unread,
Oct 23, 2002, 3:37:54 AM10/23/02
to
"Kay" <kang...@interchange.ubc.ca> wrote in message news:<ap00hu$2tc$1...@nntp.itservices.ubc.ca>...

I feel glad for your success!!

0 new messages