|
Hi,
You can use the parsing functions in string, like Split, IndexOf etc.. To parse each item in your string, assuming they are separated by space, you can do
<br />
string lineRead = streamReader.ReadLine();<br />
string []arr = lineRead.Split(" ");<br />
For converting a string to an int, you can use the Int32.Parse method.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
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
{
class Client
{
static NetworkStream output;
static BinaryWriter writer;
static BinaryReader reader;
static Thread readThread;
[STAThread]
static void Main(string[] args)
{
string msg;
TcpClient client;
client = new TcpClient();
client.Connect( "localhost", 1098 );
output = client.GetStream();
writer = new BinaryWriter(output, System.Text.Encoding.UTF8);
reader = new BinaryReader(output);
readThread = new Thread(new ThreadStart(RunClient));
readThread.Start();
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() );
}
}
}
|
|
|
|
|
Does writer.Flush() after writer.Write() in the client side help?
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Hi!
I'm not really sure if this will help you but I remember having played around with .NET<->Java communication via TCP/IP.
IIRC, the byte-order was different between .NET and Java, so if you write a 4 byte integer value, for example, you'll have to reverse the bytes on the receiver's side to get the correct value.
Could be the case with sending strings as well...
Regards,
mav
|
|
|
|
|
Not sure if that has something to do with your problem, but doesn't JAVA only use Unicode (UTF-16)?
Wouldnt that mean that receiving UTF-8 lead to problems?
Did you try switching the client's encoding to UTF-16 instead?
Cheers
Sid
|
|
|
|
|
What is bothering me is that on the Java server side the DataInputStream readUTF() method remains blocking and never returns, even with improperly formatted data. The Socket connect's and disconnect's are acknowledged though normally.
-Frank
|
|
|
|
|
Hi all,
I need to write a piece of code to check if a particular shortcut is on the desktop. If anyone knows about it, please let me know.
Thanks a bunch.
|
|
|
|
|
The file is called *shortcutname*.lnk. So just check that's on the desktop, if you just want to know it's there.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
What is the best component out there for generating thumbnails for PPT slides? I've tried Aspose... memory issues.
|
|
|
|
|
How to implement following C code in c#?
sprintf( strBuf, "0x%02x", 1 ); // then the strBuf will be "0x01" rather than "0x1"
in C# how can I specify string.Format( "0x{0}", 1 ) to generate 0x01, rather than 0x1?
thanks
|
|
|
|
|
string.Format( "0x{0:X2}", 1 );
Roger Stewart
"I Owe, I Owe, it's off to work I go..."
|
|
|
|
|
I've been doing some investigation to try to see how to avoid my IE web browser control from showing the layout while a page is downloading, so that it waits until DocumentComplete before displaying the page.
I've noticed that in C++ world, there's a flag DLCTL_SILENT that can be set on the pvarResult in IDispatch::Invoke. This flag says "No user interface will be displayed during downloads." Needless to say, this caught my attention. (If someone out there knows offhand that this is in fact *not* doing what I hope it's doing, please chime in.)
My problem now is that I can't figure out how to properly implement this in my C# application. Any ideas?
Thanks.
David
|
|
|
|
|
u can't use SuspendLayout()?
|
|
|
|
|
No, this is deep in the bowels of IE and interop. SuspendLayout() is for adding widgets to an existing control. I'm talking about actually stopping IE from dynamically showing HTML, images, etc. while it's in the process of loading it over the net. (And I'm making the assumption that this silent flag is the way to do it, which may be wrong anyway.)
This is more of a question about a) the IE SDK, and more importantly b) how to correctly use COM and interop to set the DLCTL_SILENT flag on IE from C#. In C++, it's just a matter of calling IDispatch::Invoke. But my experience with interop is limited, so I'm at a momentary standstill trying to do the equivalent Invoke operation in C#.
|
|
|
|
|
Hi,
Can anybody tell me what the difference is between an Abstract class and an interface? I believe that both dont have any implementation and you inherit them with the following syntax: MyClass: BaseClass , correct?
Thanks,
JJ
|
|
|
|
|
Abstract classes may include some implementation and some abstract members. Interfaces may include no implementation.
Neither may be directly instantiated.
The potential benefit of abstract classes is that you get the advantages of base implementation where you want it, and enforced subclass implementation where it's needed.
There is clear documentation of abstract classes and interfaces on MSDN[^].
Hope this helps.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
The basic difference between an Abstract class and an interface is that Abstract class allows Concrete method(methods that have implementation) but interface doesn't. The second difference is in terms of inheritance. A class can inherit from only one class but can implement any number of interfaces. One more difference is that interface doesn't allow variables/constants to be declared but abstract class allows for declaration of variables and constants.
------------------------------------
To study, study and only to study
|
|
|
|
|
|
|
Can't be done through WMI.
Using the GetJob APi and getting the dmCopies field of the JOB_INFO_2.DEVMODE structure works 80% of the time but if the client is using Microsoft Word it misreports the number of copies. In that case only actually parsing the spool file to get the true copy count works.
|
|
|
|
|
|
hi all,
I want to backup database and restore database using C#..
can any body please help me to do it?
Thanks in advance..
|
|
|
|
|
First, what kind of database?
Second, local backup or remote backup?
Third, "describing" backup or pure content-backup?
Cheers
Sid
|
|
|
|
|
I am using SQL Server 2000 and I can access Network SQL Servers also..
So I want to get complete database backup and restore itself in SQL Server's databases in my network..
|
|
|
|
|
Hi,
can anybody tell me that how to find out the minimum and maximum value from the single dimention array?
i don't want to go with for -loop or do while logic becoz it's taking somuch time and it reduce the performance.
can anybody tell me that what is the direct way to find out min & max value from array?
any sample code available then give me.
|
|
|
|