Thread: Help Converting Java to C

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    Question Help Converting Java to C

    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

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well, nobody is just going to write the whole code for you, you need to demonstrate some effort into making it work. As for the IDE, you can use CDT a plugin for eclipse for C/C++ if you want to stick with Eclipse.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  2. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 09:07 PM
  3. Replies: 6
    Last Post: 08-07-2003, 02:05 PM
  4. Java woes
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-06-2003, 12:37 AM
  5. converting from java
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2001, 11:17 AM