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
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
> 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
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
> 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
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
?
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
Yes.
> 2)
Yes. At least this java code works if I use java ServerSocket at the server.
> 3)
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.
Don't you think that's something that winsock does while java sockets don't?
thanks,
-Kay
thanks for all your help.
-Kay
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.
or ask them to use java.;-)
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. :-)
Any easy way to stop Java from converting the byte order?
-Kay
But be sure that the C server is doing the right thing.
I feel glad for your success!!