3

I'm trying to write a small client server program. The Server is in C#, and the client is in Java. Here are the codes:

Server:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace Server
{
    class Program
    {
        private TcpListener tcpListener;
        public static void Main(string[] args)
        {
            Program program = new Program();
            program.StartServer();

            while (true) ;
        }

        private bool StartServer()
        {
            IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];

            try
            {
                tcpListener = new TcpListener(ipAddress, 5678);
                tcpListener.Start();
                tcpListener.BeginAcceptTcpClient(new AsyncCallback(this.ProcessEvents), tcpListener);

                Console.WriteLine("Listing at Port {0}.", 5678);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return false;
            }

            return true;
        }

        private void ProcessEvents(IAsyncResult asyn)
        {
            try
            {
                TcpListener processListen = (TcpListener)asyn.AsyncState;
                TcpClient tcpClient = processListen.EndAcceptTcpClient(asyn);
                NetworkStream myStream = tcpClient.GetStream();
                if (myStream.CanRead)
                {
                    StreamReader readerStream = new StreamReader(myStream);
                    string myMessage = readerStream.ReadToEnd();
                    readerStream.Close();
                }
                myStream.Close();
                tcpClient.Close();
                tcpListener.BeginAcceptTcpClient(new AsyncCallback(this.ProcessEvents), tcpListener);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

Client:

import java.io.PrintWriter;
import java.net.Socket;


public class Program {

    public static void main(String[] args) {
        Socket socket;
        try {
            socket = new Socket( "127.0.0.1", 5678);
            PrintWriter writer = new PrintWriter(socket.getOutputStream());
            writer.print("Hello world");
            writer.flush();
            writer.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

But when I try to create a Socket in client, I get this exception:

java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Program.main(Program.java:10)

Can anyone tell me what I'm doing wrong here?

updated: I'm running x64 Windows 7 Ultimate, I don't see anything firewall message pop up (I did saw it for server once, which I allowed and set to always allow). I can connect using telenet, no problem with that. Any other suggestion please.

4
  • 3
    While it's great to see the code and the result of the client, can you provide any more context? The Java client says the connection was refused, does the server say that it's listening? With the server listening, can you telnet to that port, and verify that you do not get a connection refusal? Commented Oct 9, 2011 at 1:41
  • could it be the Windows firewall is blocking it? Commented Oct 9, 2011 at 2:28
  • Try to connect with telnet first, once connected anything you type should be printed out. However I suspect (I have not tried it) that your server will hang up as soon as a new line is detected. If you can connect with telnet then you can be fairly certain your problem lies in the code on either side. If not then your problem lies in between (firewall ? it usually is). If running on windows watch out for the windows popup to allow your program access to the network. sometimes it tends to hide behind another windows (your IDE). Commented Oct 9, 2011 at 2:41
  • I'm running x64 Windows 7 Ultimate, I don't see anything firewall message pop up (I did saw it for server once, which I allowed and set to always allow). I can connect using telenet, no problem with that. Any other suggestion please. Commented Oct 9, 2011 at 19:33

1 Answer 1

6

I have finally figured out the problem myself.

The .Net server was by default using ipv6 address, and Java client was using the ipv4 address. To create a ipv4 address use:

TcpListener tcpListener = new TcpListener(IPAddress.Any, 5678);

instead of:

IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
TcpListener tcpListener = new TcpListener(ipAddress, 5678);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.