Click here to Skip to main content
15,889,216 members
Home / Discussions / C#
   

C#

 
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 
QuestionOutput HTML table from XML file Pin
rolandvink13-May-11 11:45
rolandvink13-May-11 11:45 
AnswerRe: Output HTML table from XML file Pin
jschell13-May-11 12:25
jschell13-May-11 12:25 
GeneralRe: Output HTML table from XML file Pin
AspDotNetDev13-May-11 12:51
protectorAspDotNetDev13-May-11 12:51 
GeneralRe: Output HTML table from XML file Pin
jschell13-May-11 12:57
jschell13-May-11 12:57 
AnswerRe: Output HTML table from XML file Pin
DaveAuld13-May-11 22:50
professionalDaveAuld13-May-11 22:50 
AnswerRe: Output HTML table from XML file Pin
RaviRanjanKr16-May-11 4:37
professionalRaviRanjanKr16-May-11 4:37 
Questionsend fax with C# 2010 [modified] Pin
esialex13-May-11 2:41
esialex13-May-11 2:41 
AnswerRe: send fax with C# 2010 Pin
_Erik_13-May-11 4:19
_Erik_13-May-11 4:19 
GeneralFaxcomexlib Pin
fbova30-May-11 11:00
fbova30-May-11 11:00 
GeneralRe: Faxcomexlib Pin
_Erik_31-May-11 2:54
_Erik_31-May-11 2:54 
Questionget invalid handle when called GetWindowLong. Pin
prasadbuddhika12-May-11 23:59
prasadbuddhika12-May-11 23:59 
AnswerRe: get invalid handle when called GetWindowLong. Pin
Luc Pattyn13-May-11 1:36
sitebuilderLuc Pattyn13-May-11 1:36 
GeneralRe: get invalid handle when called GetWindowLong. Pin
prasadbuddhika13-May-11 2:21
prasadbuddhika13-May-11 2:21 
AnswerRe: get invalid handle when called GetWindowLong. Pin
Luc Pattyn13-May-11 2:39
sitebuilderLuc Pattyn13-May-11 2:39 
GeneralRe: get invalid handle when called GetWindowLong. Pin
Dave Kreskowiak13-May-11 3:33
mveDave Kreskowiak13-May-11 3:33 
QuestionProblem matching name using Regex Pin
Etienne_12312-May-11 22:54
Etienne_12312-May-11 22:54 

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.