I have written a simple networked program in Java (a server and client program) and I now need to change my client program from Java to C. I haven't had any experience in C or C++ at all, and I'm having a difficult time doing this. I don't think the best way about going about this is going line by line in the Java program and trying to convert each line of code into C. I'm not sure if that would even work well. I've attached the Java version of the client program, and if anyone is willing to help me along I would greatly appreciate it. I am going to be changing the input a little in that a user supplies the IP address and port number of the server via the command line (which actually I know how to do).
Also, can anyone recommend a nice editor to work with C and C++? I currently use Eclipse when writing in Java, and I like that a lot.
Code:
// MsgClient.java
import java.io.*;
import java.net.*;
import java.util.*;
public class MsgClientTCP {
private static final int PORT = 1234; // server details
private static final String HOST = "localhost";
private Socket sock;
private BufferedReader in; // i/o for the client
private PrintWriter out;
public MsgClientTCP() {
makeContact();
userInput();
}
private void makeContact() {
try {
sock = new Socket(HOST, PORT);
in = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
out = new PrintWriter(sock.getOutputStream(), true); // autoflush
} catch (Exception e) {
System.out.println(e);
}
} // end of makeContact()
public void userInput() {
Scanner scan = new Scanner(System.in);
while(true) {
System.out.println("Enter a command (send, receive, or exit): ");
String command = scan.next();
if(command.equalsIgnoreCase("send")) {
sendMessage();
}
else if(command.equalsIgnoreCase("receive")){
sendGet();
}
else if(command.equalsIgnoreCase("exit")) {
System.exit(0);
}
else System.out.println("Command not recognized! Please enter only send, receive, or exit: ");
}
}
private void sendGet() {
// Send "get" command, read response and display it
// Response should be "HIGH$$ n1 & s1 & .... nN & sN & "
try {
out.println("get");
String line = in.readLine();
//System.out.println(line);
if ((line.length() >= 7) && // "HIGH$$ "
(line.substring(0, 6).equals("HIGH$$")))
showHigh(line.substring(6).trim());
// remove HIGH$$ keyword and surrounding spaces
else
// should not happen
System.out.println(line + "\n");
} catch (Exception ex) {
System.out.println("Problem obtaining high scores\n");
System.out.println(ex);
}
} // end of sendGet()
private void showHigh(String line)
// Parse the high scores and display in a nicer way
{
Scanner scan = new Scanner(System.in);
StringTokenizer st = new StringTokenizer(line, "&");
String name, message;
String nameLookup;
System.out.println("Please enter nickname to lookup: ");
nameLookup = scan.next();
boolean found = false;
try {
while (st.hasMoreTokens()) {
name = st.nextToken().trim();
message = st.nextToken().trim();
if (name.toLowerCase().equals(nameLookup.toLowerCase())) {
System.out.println(name + " : " + message + "\n");
found = true;
}
}
} catch (Exception e) {
System.out.println("Error: \n" + e);
}
if (found == false)
System.out.println("Nickname not found!" + "\n");
} // end of showHigh()
private void sendMessage() {
String name;
String message;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter nickname: ");
name = scan.next();
System.out.println("Please enter desired message: ");
message = scan.next();
out.println("score " + name + " & " + message + " &");
// jtaMesgs.append("Sent " + name + " & " + message + "\n");
}
// end of sendScore()
// ------------------------------------
public static void main(String args[]) {
new MsgClientTCP();
}
} // end of ScoreClient class