Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help me to convert this code in Java into a C# code

Help me to convert this code in Java into a C# code

Scheduled Pinned Locked Moved C#
csharpjavasysadminhelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 3332887
    wrote on last edited by
    #1

    Help me to convert this code in Java into a C# code

    import java.io.*;
    import java.net.*;

    public class simpleServer
    {
    public static void main(String args[])
    {
    // Message terminator
    char EOF = (char)0x00;

    	try
    	{
    		// create a serverSocket connection on port 9999
    		ServerSocket s = new ServerSocket(2055);
    
    		System.out.println("Server started. Waiting for connections...");
    		// wait for incoming connections
    		Socket incoming = s.accept();
    
    		BufferedReader data\_in = new BufferedReader(
    				new InputStreamReader(incoming.getInputStream()));
    		PrintWriter data\_out = new PrintWriter(incoming.getOutputStream());
    
    		data\_out.println("Welcome! type EXIT to quit." + EOF);
    		data\_out.flush();
    
    		boolean quit = false;
    
    		// Waits for the EXIT command
    		while (!quit)
    		{
    			String msg = data\_in.readLine();
    
    			if (msg == null) quit = true;
    
    			if (!msg.trim().equals("EXIT"))
    			{
    				data\_out.println("You sayed: <b>" + msg.trim() + "</b>" + EOF);
    				data\_out.flush();
    			}
    			else
    			{
    				quit = true;
    			}
    		}
    	}
    	catch (Exception e)
    	{
    		System.out.println("Connection lost");
    	}
    }
    

    }

    OriginalGriffO U N 3 Replies Last reply
    0
    • U User 3332887

      Help me to convert this code in Java into a C# code

      import java.io.*;
      import java.net.*;

      public class simpleServer
      {
      public static void main(String args[])
      {
      // Message terminator
      char EOF = (char)0x00;

      	try
      	{
      		// create a serverSocket connection on port 9999
      		ServerSocket s = new ServerSocket(2055);
      
      		System.out.println("Server started. Waiting for connections...");
      		// wait for incoming connections
      		Socket incoming = s.accept();
      
      		BufferedReader data\_in = new BufferedReader(
      				new InputStreamReader(incoming.getInputStream()));
      		PrintWriter data\_out = new PrintWriter(incoming.getOutputStream());
      
      		data\_out.println("Welcome! type EXIT to quit." + EOF);
      		data\_out.flush();
      
      		boolean quit = false;
      
      		// Waits for the EXIT command
      		while (!quit)
      		{
      			String msg = data\_in.readLine();
      
      			if (msg == null) quit = true;
      
      			if (!msg.trim().equals("EXIT"))
      			{
      				data\_out.println("You sayed: <b>" + msg.trim() + "</b>" + EOF);
      				data\_out.flush();
      			}
      			else
      			{
      				quit = true;
      			}
      		}
      	}
      	catch (Exception e)
      	{
      		System.out.println("Connection lost");
      	}
      }
      

      }

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      You forgot to say "pretty please?". I'm going to sulk, now. :(

      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • U User 3332887

        Help me to convert this code in Java into a C# code

        import java.io.*;
        import java.net.*;

        public class simpleServer
        {
        public static void main(String args[])
        {
        // Message terminator
        char EOF = (char)0x00;

        	try
        	{
        		// create a serverSocket connection on port 9999
        		ServerSocket s = new ServerSocket(2055);
        
        		System.out.println("Server started. Waiting for connections...");
        		// wait for incoming connections
        		Socket incoming = s.accept();
        
        		BufferedReader data\_in = new BufferedReader(
        				new InputStreamReader(incoming.getInputStream()));
        		PrintWriter data\_out = new PrintWriter(incoming.getOutputStream());
        
        		data\_out.println("Welcome! type EXIT to quit." + EOF);
        		data\_out.flush();
        
        		boolean quit = false;
        
        		// Waits for the EXIT command
        		while (!quit)
        		{
        			String msg = data\_in.readLine();
        
        			if (msg == null) quit = true;
        
        			if (!msg.trim().equals("EXIT"))
        			{
        				data\_out.println("You sayed: <b>" + msg.trim() + "</b>" + EOF);
        				data\_out.flush();
        			}
        			else
        			{
        				quit = true;
        			}
        		}
        	}
        	catch (Exception e)
        	{
        		System.out.println("Connection lost");
        	}
        }
        

        }

        U Offline
        U Offline
        User 3332887
        wrote on last edited by
        #3

        help me please :)

        D 1 Reply Last reply
        0
        • U User 3332887

          help me please :)

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          Sure. Show me what you've done yourself, and point out which bits aren't functioning as you expect, and I'll help you fix them. I'm not however going to do all the work for you.

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          U 1 Reply Last reply
          0
          • U User 3332887

            Help me to convert this code in Java into a C# code

            import java.io.*;
            import java.net.*;

            public class simpleServer
            {
            public static void main(String args[])
            {
            // Message terminator
            char EOF = (char)0x00;

            	try
            	{
            		// create a serverSocket connection on port 9999
            		ServerSocket s = new ServerSocket(2055);
            
            		System.out.println("Server started. Waiting for connections...");
            		// wait for incoming connections
            		Socket incoming = s.accept();
            
            		BufferedReader data\_in = new BufferedReader(
            				new InputStreamReader(incoming.getInputStream()));
            		PrintWriter data\_out = new PrintWriter(incoming.getOutputStream());
            
            		data\_out.println("Welcome! type EXIT to quit." + EOF);
            		data\_out.flush();
            
            		boolean quit = false;
            
            		// Waits for the EXIT command
            		while (!quit)
            		{
            			String msg = data\_in.readLine();
            
            			if (msg == null) quit = true;
            
            			if (!msg.trim().equals("EXIT"))
            			{
            				data\_out.println("You sayed: <b>" + msg.trim() + "</b>" + EOF);
            				data\_out.flush();
            			}
            			else
            			{
            				quit = true;
            			}
            		}
            	}
            	catch (Exception e)
            	{
            		System.out.println("Connection lost");
            	}
            }
            

            }

            N Offline
            N Offline
            Nathan Revka
            wrote on last edited by
            #5

            Why do you need to convert it? Just make it a method and use it, C#.NET allows you to mix languages.

            1 Reply Last reply
            0
            • D DaveyM69

              Sure. Show me what you've done yourself, and point out which bits aren't functioning as you expect, and I'll help you fix them. I'm not however going to do all the work for you.

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
              Why are you using VB6? Do you hate yourself? (Christian Graus)

              U Offline
              U Offline
              User 3332887
              wrote on last edited by
              #6

              I have tried in this mode:

              using System;
              using System.IO;
              using System.Net;
              using System.Net.Sockets;
              using System.Configuration;

              class Server
              {
              public static void Main()
              {
              // Message terminator
              char EOF = (char)0x00;

              try
              {
              IPAddress ipAd = IPAddress.Parse("127.0.0.1");

              /* Initializes the Listener */
              TcpListener myList = new TcpListener(ipAd, 2055);

              /* Start Listeneting at the specified port */
              myList.Start();

              Console.WriteLine("The server is running. Waiting for a connection....");

              Socket soc = myList.AcceptSocket();

              Stream s = new NetworkStream(soc);
              StreamReader sr = new StreamReader(s);
              StreamWriter sw = new StreamWriter(s);

              sw.WriteLine("Benvenuti sul server! digita EXIT per disconnetterti." + EOF);
              sw.Flush();

              bool quit = false;

              while (!quit)
              {
              string msg = sr.ReadLine();

              if (msg == null) quit = true;

              if (msg != "EXIT")
              {
              sw.WriteLine("Guest: <b>" + msg + "</b>" + EOF);
              sw.Flush();
              }
              else
              {
              quit = true;
              }

              } // end while
              }
              catch (Exception e)
              {
              Console.WriteLine(e.Message);

              }
              }
              }

              D 1 Reply Last reply
              0
              • U User 3332887

                I have tried in this mode:

                using System;
                using System.IO;
                using System.Net;
                using System.Net.Sockets;
                using System.Configuration;

                class Server
                {
                public static void Main()
                {
                // Message terminator
                char EOF = (char)0x00;

                try
                {
                IPAddress ipAd = IPAddress.Parse("127.0.0.1");

                /* Initializes the Listener */
                TcpListener myList = new TcpListener(ipAd, 2055);

                /* Start Listeneting at the specified port */
                myList.Start();

                Console.WriteLine("The server is running. Waiting for a connection....");

                Socket soc = myList.AcceptSocket();

                Stream s = new NetworkStream(soc);
                StreamReader sr = new StreamReader(s);
                StreamWriter sw = new StreamWriter(s);

                sw.WriteLine("Benvenuti sul server! digita EXIT per disconnetterti." + EOF);
                sw.Flush();

                bool quit = false;

                while (!quit)
                {
                string msg = sr.ReadLine();

                if (msg == null) quit = true;

                if (msg != "EXIT")
                {
                sw.WriteLine("Guest: <b>" + msg + "</b>" + EOF);
                sw.Flush();
                }
                else
                {
                quit = true;
                }

                } // end while
                }
                catch (Exception e)
                {
                Console.WriteLine(e.Message);

                }
                }
                }

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                That compiles with no errors or warnings. So, what's the problem?

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                Why are you using VB6? Do you hate yourself? (Christian Graus)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups