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.
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);
精彩评论