Click here to Skip to main content
15,907,000 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Project for different OS Pin
DavidNohejl29-Mar-05 21:48
DavidNohejl29-Mar-05 21:48 
GeneralRe: C# Project for different OS Pin
bosfan30-Mar-05 0:58
bosfan30-Mar-05 0:58 
GeneralRe: C# Project for different OS Pin
Stanciu Vlad29-Mar-05 22:01
Stanciu Vlad29-Mar-05 22:01 
GeneralAdding Mails to a Folder in Outlook programmatically Pin
ShwetaRokade29-Mar-05 19:55
ShwetaRokade29-Mar-05 19:55 
GeneralA new question Pin
shaima'29-Mar-05 19:43
shaima'29-Mar-05 19:43 
GeneralRerading a Text File with Advance Options.... Pin
Anonymous29-Mar-05 19:17
Anonymous29-Mar-05 19:17 
GeneralRe: Rerading a Text File with Advance Options.... Pin
S. Senthil Kumar29-Mar-05 19:24
S. Senthil Kumar29-Mar-05 19:24 
GeneralCSharp Client to Java Server TCP problems Pin
fperugini29-Mar-05 15:30
fperugini29-Mar-05 15:30 
Hello fellow C# friends,

I am trying to write a C# TCP client that was formerly written in Java.
The server must still remain in Java.

I cannot get text data from the C# client to be received by the Java
TCP server. No matter how I try to send data from the client to the
server, the Java server DataInputStream readUTF() method never returns
with any data. Can someone please shed some light on this problem?
Thanks.

-Frank

I re-wrote them both in simpler form for demonstration purposes.

Java Based Server: 
---------------------- 
import java.net.*; 
import java.io.*; 
import java.util.*; 

public class ChatServer { 
  public ChatServer (int port) throws IOException { 
    ServerSocket server = new ServerSocket (port); 
    while (true) { 
      Socket client = server.accept (); 
      System.out.println ("Accepted from " + client.getInetAddress ()); 
      ChatHandler c = new ChatHandler (client); 
      c.start (); 
    } 
  } 

  public static void main (String args[]) throws IOException { 
    new ChatServer (1098); 
  } 
} 


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

public class ChatHandler extends Thread { 
  protected Socket s; 
  protected DataInputStream i; 
  protected DataOutputStream o; 

  public ChatHandler (Socket s) throws IOException { 
    this.s = s; 
    i = new DataInputStream (new BufferedInputStream 
(s.getInputStream())); 
    o = new DataOutputStream (new BufferedOutputStream 
(s.getOutputStream())); 
  } 

  protected static Vector handlers = new Vector (); 

  public void run () { 
    String name = s.getInetAddress ().toString (); 
    try { 
      System.out.println(name + " has joined."); 
      handlers.addElement (this); 
      while (true) { 
        System.out.println("Waiting for data..."); 
        String msg = i.readUTF (); 
        System.out.println(name + " - " + msg); 
      } 
    } catch (IOException ex) { 
      ex.printStackTrace (); 
    } finally { 
      handlers.removeElement (this); 
      System.out.println(name + " has left."); 
      try { 
        s.close (); 
      } catch (IOException ex) { 
        ex.printStackTrace(); 
      } 
    } 
  } 

C# Client 
---------------------- 
using System; 
using System.Threading; 
using System.Net.Sockets; 
using System.IO; 
using System.Data; 
using System.Net; 
using System.Text; 

namespace TCPClient 
{ 
        /// <summary> 
        /// Summary description for Class1. 
        /// </summary> 
        class Client 
        { 
                static NetworkStream output; 
                static BinaryWriter writer; 
                static BinaryReader reader; 
                static Thread readThread; 


                /// <summary> 
                /// The main entry point for the application. 
                /// </summary> 
                [STAThread] 
                static void Main(string[] args) 
                { 
                        string msg; 
                        TcpClient client; 


                        client = new TcpClient(); 
                        client.Connect( "localhost", 1098 ); 
                        output = client.GetStream(); 


                        // create objects for writing and reading across stream 
                        writer = new BinaryWriter(output, System.Text.Encoding.UTF8); 
                        reader = new BinaryReader(output); 


                        //Start thread to recieve data back from server... 
                        readThread = new Thread(new ThreadStart(RunClient)); 
                        readThread.Start(); 


                        // get text from colsole to send until just the ENTER key pressed... 


                        do 
                        { 


                                Console.Write("Text to send: "); 
                                msg = Console.ReadLine(); 
                                if (msg == "") 
                                        break; 


                                Byte[] byteDateLine = Encoding.ASCII.GetBytes( msg.ToCharArray() ); 
                                writer.Write(byteDateLine, byteDateLine.Length, 0 ); 


                        } while (true); 


                        Console.WriteLine("Closing connection"); 


                        readThread.Abort(); 
                        writer.Close(); 
                        reader.Close(); 
                        output.Close(); 
                        client.Close(); 
                } 


                static void RunClient() 
                { 
                        string msg; 


                        try 
                        { 
                                Console.WriteLine("Waiting for data from server..."); 
                                do 
                                { 
                                        msg = reader.ReadString(); 
                                        Console.WriteLine(msg); 
                                } while (true); 


                        } 
                        catch ( Exception error ) 
                        { 
                                Console.WriteLine( error.ToString() ); 
                        } 


                } 

        } 

GeneralRe: CSharp Client to Java Server TCP problems Pin
S. Senthil Kumar29-Mar-05 19:27
S. Senthil Kumar29-Mar-05 19:27 
GeneralRe: CSharp Client to Java Server TCP problems Pin
mav.northwind29-Mar-05 20:21
mav.northwind29-Mar-05 20:21 
GeneralRe: CSharp Client to Java Server TCP problems Pin
Sebastian Schneider29-Mar-05 21:14
Sebastian Schneider29-Mar-05 21:14 
GeneralRe: CSharp Client to Java Server TCP problems Pin
fperugini30-Mar-05 1:51
fperugini30-Mar-05 1:51 
GeneralCheck if a shortcut is on desktop Pin
Johny Ng29-Mar-05 14:44
Johny Ng29-Mar-05 14:44 
GeneralRe: Check if a shortcut is on desktop Pin
Christian Graus29-Mar-05 16:54
protectorChristian Graus29-Mar-05 16:54 
GeneralPowerPoint thumbnail generation Pin
Alex Korchemniy29-Mar-05 14:17
Alex Korchemniy29-Mar-05 14:17 
Generalstring.Format Pin
LeeeNN29-Mar-05 7:19
LeeeNN29-Mar-05 7:19 
GeneralRe: string.Format Pin
Roger Stewart29-Mar-05 7:49
professionalRoger Stewart29-Mar-05 7:49 
Generalsetting DLCTL_SILENT on WebBrowser control Pin
dkarlton29-Mar-05 6:13
dkarlton29-Mar-05 6:13 
GeneralRe: setting DLCTL_SILENT on WebBrowser control Pin
spif200130-Mar-05 1:39
spif200130-Mar-05 1:39 
GeneralRe: setting DLCTL_SILENT on WebBrowser control Pin
dkarlton30-Mar-05 4:15
dkarlton30-Mar-05 4:15 
GeneralAbstract class and interfaces Pin
MrJJKoolJ29-Mar-05 5:45
MrJJKoolJ29-Mar-05 5:45 
GeneralRe: Abstract class and interfaces Pin
turbochimp29-Mar-05 6:10
turbochimp29-Mar-05 6:10 
GeneralRe: Abstract class and interfaces Pin
_J_29-Mar-05 6:16
_J_29-Mar-05 6:16 
GeneralRe: Abstract class and interfaces Pin
S. Senthil Kumar29-Mar-05 19:31
S. Senthil Kumar29-Mar-05 19:31 
General[Message Deleted] Pin
nobody262629-Mar-05 4:52
nobody262629-Mar-05 4:52 

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.