Click here to Skip to main content
15,902,809 members
Home / Discussions / C#
   

C#

 
AnswerRe: how to switch off the parallel port Pin
OriginalGriff14-May-11 4:07
mveOriginalGriff14-May-11 4:07 
GeneralRe: how to switch off the parallel port Pin
aeman14-May-11 4:33
aeman14-May-11 4:33 
AnswerRe: how to switch off the parallel port Pin
OriginalGriff14-May-11 4:40
mveOriginalGriff14-May-11 4:40 
GeneralRe: how to switch off the parallel port Pin
aeman14-May-11 4:54
aeman14-May-11 4:54 
AnswerRe: how to switch off the parallel port Pin
Luc Pattyn14-May-11 2:01
sitebuilderLuc Pattyn14-May-11 2:01 
AnswerRe: how to switch off the parallel port Pin
Dave Kreskowiak14-May-11 3:19
mveDave Kreskowiak14-May-11 3:19 
GeneralRe: how to switch off the parallel port Pin
PIEBALDconsult14-May-11 4:54
mvePIEBALDconsult14-May-11 4:54 
GeneralRe: how to switch off the parallel port Pin
Dan Mos14-May-11 12:49
Dan Mos14-May-11 12:49 
AnswerRe: how to switch off the parallel port Pin
Dave Kreskowiak14-May-11 5:32
mveDave Kreskowiak14-May-11 5:32 
QuestionHow do I install a game I've made in XNA(using Visual Studio 2008) on a computer that does not have Visual Studio? Pin
illuitionist13-May-11 16:34
illuitionist13-May-11 16:34 
AnswerRe: How do I install a game I've made in XNA(using Visual Studio 2008) on a computer that does not have Visual Studio? Pin
Luc Pattyn13-May-11 17:47
sitebuilderLuc Pattyn13-May-11 17:47 
AnswerRe: How do I install a game I've made in XNA(using Visual Studio 2008) on a computer that does not have Visual Studio? Pin
Mark Salsbery13-May-11 18:05
Mark Salsbery13-May-11 18:05 
AnswerRe: How do I install a game I've made in XNA(using Visual Studio 2008) on a computer that does not have Visual Studio? Pin
bblu_sahu13-May-11 21:07
bblu_sahu13-May-11 21:07 
GeneralRe: How do I install a game I've made in XNA(using Visual Studio 2008) on a computer that does not have Visual Studio? Pin
Mark Salsbery13-May-11 21:24
Mark Salsbery13-May-11 21:24 
QuestionSockets: Send strings from Java to C#? Pin
CodeGust13-May-11 15:02
CodeGust13-May-11 15:02 
AnswerRe: Sockets: Send strings from Java to C#? Pin
Luc Pattyn13-May-11 15:55
sitebuilderLuc Pattyn13-May-11 15:55 
GeneralRe: Sockets: Send strings from Java to C#? Pin
CodeGust13-May-11 16:02
CodeGust13-May-11 16:02 
AnswerRe: Sockets: Send strings from Java to C#? Pin
Luc Pattyn13-May-11 16:35
sitebuilderLuc Pattyn13-May-11 16:35 
GeneralRe: Sockets: Send strings from Java to C#? Pin
Daniel Scott13-May-11 23:43
Daniel Scott13-May-11 23:43 
GeneralRe: Sockets: Send strings from Java to C#? Pin
Mark Salsbery13-May-11 18:11
Mark Salsbery13-May-11 18:11 
GeneralRe: Sockets: Send strings from Java to C#? Pin
Richard MacCutchan13-May-11 22:12
mveRichard MacCutchan13-May-11 22:12 
GeneralRe: Sockets: Send strings from Java to C#? Pin
Mark Salsbery13-May-11 22:20
Mark Salsbery13-May-11 22:20 
GeneralRe: Sockets: Send strings from Java to C#? Pin
Richard MacCutchan13-May-11 23:33
mveRichard MacCutchan13-May-11 23:33 
AnswerRe: Sockets: Send strings from Java to C#? Pin
CodeGust15-May-11 19:19
CodeGust15-May-11 19:19 
So here's my c# code:

class Program
    {
        private StreamWriter swSender;
        private StreamReader srReceiver;
        private TcpClient tcpServer;
        private Thread thrMessaging;
        private IPAddress ipAddr;
        private bool Connected;

        private string receivedData;

        static void Main(string[] args)
        {
            System.Diagnostics.Process.Start(Path.GetFullPath("RunJar.bat"));
            Console.WriteLine("C# Sockets Program has started.");

            try
            {
                Program prog = new Program();
                prog.InitializeConnection();
            }
            catch (Exception e) { Console.WriteLine(e); }

            Console.ReadLine();
        }


        private void InitializeConnection()
        {
            // Parse the IP address
            string ipAdress = "127.0.0.1";
            ipAddr = IPAddress.Parse(ipAdress);

            // Start a new TCP connections to the chat server
            tcpServer = new TcpClient();
            try
            {
                tcpServer.Connect(ipAddr, 1800);
                swSender = new StreamWriter(tcpServer.GetStream());

                // Start the thread for receiving messages and further communication
                thrMessaging = new Thread(new ThreadStart(ReceiveMessages));
                thrMessaging.Start();
                Connected = true;
            }
            catch (Exception e2)
            {
                Console.WriteLine(e2.ToString());
            }
        }

        private void ReceiveMessages()
        {
            // Receive the response from the server
            srReceiver = new StreamReader(tcpServer.GetStream());
            while (Connected)
            {
                String con = srReceiver.ReadLine();
                string StringMessage = HttpUtility.UrlDecode(con, System.Text.Encoding.UTF8);

                processMessage(StringMessage);
            }
        }

        private void processMessage(String p)
        {
            // do something with the message
            Console.WriteLine(p);
        }

        private void SendMessage(String p)
        {
            if (p != "")
            {
                p = HttpUtility.UrlEncode(p, System.Text.Encoding.UTF8);
                swSender.WriteLine(p);
                swSender.Flush();
            }
        }

    }


And here's my Java code:
public class JavaSocket {

 public void runJavaSocket() {
  
  System.out.println("Java Sockets Program has started.");
  
  try {
   Socket s = new Socket("127.0.0.1", 1800);
   s.connect(new InetSocketAddress("127.0.0.1", 1800), 10000);
   while (!s.isConnected()) {
    s.connect(new InetSocketAddress("127.0.0.1", 1800), 10000);
   }
   System.out.println("java Socket Connected.");
      OutputStream out = s.getOutputStream();
      out.write("helloFromjavaCode".getBytes());
      out.flush();
      out.close();
   
   
   /*
   ServerSocket ss = new ServerSocket(1800);
   Socket s = ss.accept();
   System.out.println("Client Accepted");
   BufferedReader br = new BufferedReader(new InputStreamReader(
     s.getInputStream()));
   System.out.println(br.readLine());
   PrintWriter wr = new PrintWriter(new OutputStreamWriter(
     s.getOutputStream()), true);
   wr.println("Welcome to Socket Programming");
   */
  } catch (Exception e) {
   System.out.println(e);
  }
  
  try {
   @SuppressWarnings("unused")
   int a = System.in.read();
  }
  catch (Exception e) {}
 }

}


Here's the exception that c# program gives:

C# Sockets Program has started.
System.Net.Sockets.SocketException (0x80004005): No connection could be made bec
ause the target machine actively refused it 127.0.0.1:1800
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
ss socketAddress)
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
at System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP)
at System.Net.Sockets.TcpClient.Connect(IPAddress address, Int32 port)
at CsSocket.Program.InitializeConnection() in C:\Users\Sergii\documents\visua
l studio 2010\Projects\CsSocket\CsSocket\Program.cs:line 66

Here's the exception that Java program gives:
java.net.ConnectException: Connection refused: connect


What is wrong with my code?
AnswerRe: Sockets: Send strings from Java to C#? Pin
CodeGust16-May-11 14:53
CodeGust16-May-11 14:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.