Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

code convertion from c to java

please can someone help me convert this socket program to java

/* Client code in C */

#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h>

int main(void) { struct sockaddr_in stSockAddr; int Res; int SocketFD = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (-1 == SocketFD)
{
  perror("cannot create socket");
  exit(EXIT_FAILURE);
}

memset(&stSockAddr, 0, sizeof(stSockAddr));

stSockAddr.sin_family = AF_INET;
stSockAddr.sin_port = htons(1100);
Res = inet_pton(AF_INET, "192.168.1.3", &stSockAddr.sin_addr);

if (0 > Res)
{
  perror("error: first parameter is not a valid address family");
  close(SocketFD);
  exit(EXIT_FAILURE);
}
else if (0 == Res)
{
  perror("char string (second parameter does not contain valid ipaddress)");
  close(SocketFD);
  exit(EXIT_FAILURE);
}

if (-1 == connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr)))
{
  perror("connect failed");
  close(SocketFD);
  exit(EXIT_FAILURE);
}

/* perform read write operations ... */

shutdown(SocketFD, SHUT_RDWR);

close(SocketFD);
return 0;

}

is anybody there to help out?

It would be great if you could tell us what exactly are you trying to achieve in the program. Java has its own packages for Sockets, but just a gist would help understand better.

1 Answer

You have probably figured out what you wanted to do by now, but for the sake of future viewers of this topic; there is already a Socket class in Java that should suite most, if not all, of your needs.