I need some help. I have to translate a c app into java. Which would be no problem if I knew c. Is there a good uber begginers level tutorial somewhere, that i might be able to figure out what this c code is doing? I mean, i am seeing things like "struct" which I have no idea what that is and pointers which java doesnt use. Anyone have an idea of where i should look.
sj87 19 Light Poster
I need some help. I have to translate a c app into java. Which would be no problem if I knew c. Is there a good uber begginers level tutorial somewhere, that i might be able to figure out what this c code is doing? I mean, i am seeing things like "struct" which I have no idea what that is and pointers which java doesnt use. Anyone have an idea of where i should look.
I think you'll have a big problem there. Most probably you will be able to make the same program done in C, but in Java. The problem is that it won't be like "translating" english to spanish. Firs of all, C is an imperative language, while Java is an object oriented one. Like you've said, you dont have pointers in java, and thats only one of the "heavy" differences if you've never used them. For instance, in C you don't have objects ( you can try to simulate this concept using ADT's, but thats another subject ), you dont have classes, inheritance or polymorfism and another bunch of difs. So, as you can see, its a very different languaje, from several points of view. I don't know how complex its the code you have to "translate" but you could start by researching a bit about C using your best friend Google. Then you can ask whatever you don't understand about it and if you still cant figure what the purpose of that code is, you can send it to me and I'll try to do my best to help you. Good Luck!
PS: I skipped the explanation about structs because you need to know some previous stuff before getting there. Look for tutorials!! You'll find a lot!
taconator 0 Newbie Poster
Hey, Thanks for the info. I would take you up on sending you the c code, but it is a homework assignment, and I dont want you to think I am fishing for someone to do it for me. Having said that, if I did send it to you, all I really need is an explanation of what the code is doing. Once I know what the c code is doing, then i can easilly (hopefully) re-write another server / client app in java. I will post the c code if you request, but I really dont want you to think i am looking for an easy out. I want to do it, but I just dont know c code from a hole in the ground. Actually i will attach a text file that has two c classes(if they are called classes), but i put them into one text file.
------The Client---------
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define SERVER_PORT 5432
#define MAX_LINE 256
int
main(int argc, char * argv[]){
FILE *fp;
struct hostent *hp;
struct sockaddr_in sin;
char *host;
char buf[MAX_LINE];
int s;
int len;
if (argc==2) {
host = argv[1];
}
else {
fprintf(stderr, "usage: simplex-talk host\n");
exit(1);
}
/* translate host name into peers IP address */
hp = gethostbyname(host);
if (!hp) {
fprintf(stderr, "simplex-talk: unknown host: %s\n", host);
exit(1);
}
/* build address data structure */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
sin.sin_port = htons(SERVER_PORT);
/* active open */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("simplex-talk: socket");
exit(1);
}
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("simplex-talk: connect");
close(s);
exit(1);
}
/* main loop: get and send lines of text */
while (fgets(buf, sizeof(buf), stdin)) {
buf[MAX_LINE-1] = \0;
len = strlen(buf) + 1;
send(s, buf, len, 0);
}
}
-----End Client----------
-----The Server---------
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define SERVER_PORT 5432
#define MAX_PENDING 5
#define MAX_LINE 256
int
main(){
struct sockaddr_in sin;
char buf[MAX_LINE];
int len;
int s, new_s;
/* build address data structure */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(SERVER_PORT);
/* setup passive open */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("simplex-talk: socket");
exit(1);
}
if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) < 0) {
perror("simplex-talk: bind");
exit(1);
}
listen(s, MAX_PENDING);
/* wait for connection, then receive and print text */
while(1) {
if ((new_s = accept(s, (struct sockaddr *)&sin, &len)) < 0) {
perror("simplex-talk: accept");
exit(1);
}
while (len = recv(new_s, buf, sizeof(buf), 0))
fputs(buf, stdout);
close(new_s);
}
}
------End Server-----
sj87 19 Light Poster
Hey, Thanks for the info. I would take you up on sending you the c code, but it is a homework assignment, and I dont want you to think I am fishing for someone to do it for me. Having said that, if I did send it to you, all I really need is an explanation of what the code is doing. Once I know what the c code is doing, then i can easilly (hopefully) re-write another server / client app in java. I will post the c code if you request, but I really dont want you to think i am looking for an easy out. I want to do it, but I just dont know c code from a hole in the ground. Actually i will attach a text file that has two c classes(if they are called classes), but i put them into one text file.
Your welcome! Please, don't think I was trying to make you take the easy way. As I told you in my previous post, you should try finding a basic tutorial in order to get some information about how is data representation in C, reading about stuff like: built-in types, arrays, pointers, structs & unions, types definition using typedef, etc. After that I recommend you looking for some information ( know i've seen the code, I can lead you a bit more ) about socket programming in C. I dont pretend you to know how to program this stuff by yourself but if you get an idea of what is the operational insight of some functions like socket
I think you will get the main idea of the code. If you have tried with that and you still want my help, I can tell you the "idea" of both client & server. Good Luck!
PS: What you've put in that file are not classes, they are functions. In particular they are the main functions of each program ( client & server ). Each program coded in C, MUST have a main function because thats the entry point of the program( like in java, but in C is just one more function ).
lookof2day 6 Junior Poster in Training
Java provides its own set of libraries for Socket Programming. Refer java.net package. The code provided by you uses C specific libraries for socket programming.
However, the program has two parts Client and Server. The client reads two arguments from command line. They are the program/file name and the host with which you want to connect to. In case of Java, you only require one argument i.e., the host-name.
It then opens a Socket to communicate to the Server.
The program can then uses an infinite loop to get and send messages from and two Server.
The Server part also does some sort of similar stuff. It does not require any host name as it is itself the host.
The Server binds itself to a socket so that it can listen to requests and process them.
The same can be achieved very easily in Java. What you here need to understand is the concept that Java and C uses for Socket programming. You cannot straight away convert a C program to Java because of the differences in them, their architecture and the libraries used by them.
taconator 0 Newbie Poster
thank you all for the explanations. Currently i have written two small java classes using java.net libraries. I am unsure if i need to create threads to start everything off or not but the client handles reading the input from the command line, puts it into a datagram packet, and sends it off to the server. The server recieves it, gets the data, ip, etc.. In the c code what does it do with the data it recieves? Does it just echo it back to the client? That is what i am assuming from the comments. Again, thank you all for explaining this very simple c stuff to me. I feel kinda retarded having to ask for someone to explain such a thing to me. I will finish the java stuff this weekend and i will post what I have so you guys can critique me.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.