|
It's been wrapped and/or implemented by frameworks like Win32 (WinSock), Java,.NET, etc., but Berkeley sockets was originally C. Just sayin...
Of course, "socket" these days pretty much is used generically to describe a communication endpoint.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Point taken.
The best things in life are not things.
|
|
|
|
|
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()
{
string ipAdress = "127.0.0.1";
ipAddr = IPAddress.Parse(ipAdress);
tcpServer = new TcpClient();
try
{
tcpServer.Connect(ipAddr, 1800);
swSender = new StreamWriter(tcpServer.GetStream());
thrMessaging = new Thread(new ThreadStart(ReceiveMessages));
thrMessaging.Start();
Connected = true;
}
catch (Exception e2)
{
Console.WriteLine(e2.ToString());
}
}
private void ReceiveMessages()
{
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)
{
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();
} 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?
|
|
|
|
|
Thank everyone for trying to help! I think I've found a good, working solution. Using UDP sockets:
Java code:
public void runJavaSocket() {
System.out.println("Java Sockets Program has started."); int i=0;
try {
DatagramSocket socket = new DatagramSocket();
System.out.println("Sending the udp socket...");
socket.send(toDatagram("HI",InetAddress.getByName("127.0.0.1"),3800));
while (true)
{
System.out.println("Sending hi " + i);
Thread.currentThread();
Thread.sleep(1000);
socket.send(toDatagram("HI " + String.valueOf(i),InetAddress.getByName("127.0.0.1"),3800));
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public DatagramPacket toDatagram(
String s, InetAddress destIA, int destPort) {
byte[] buf = new byte[s.length() + 1];
s.getBytes(0, s.length(), buf, 0);
return new DatagramPacket(buf, buf.length,
destIA, destPort);
}
C# code:
string returnData;
byte[] receiveBytes;
using (UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3800)))
{
IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3800);
while (true)
{
receiveBytes = udpClient.Receive(ref remoteIpEndPoint);
returnData = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine(returnData);
}
}
|
|
|
|
|
I need to read an XML file and output it as an HTML table. I have a background in Access, SQL and Crystal Reports so have experience with coding and relational databases. I'm now developing an application in Sharepoint using C# reading an XML file which I am unfamiliar with. The XML file has a structure as below:
<?xml version="1.0" encoding="ISO-8859-1"?>
<loan>
<library>
<libraryName>Auckland Library</libraryName>
<item>
<duedate>24-05-2011</duedate>
<title>Empire Burlesque</title>
</item>
<item>
<duedate>12-05-2011</duedate>
<title>Hide your Heart</title>
</item>
</library>
<library>
<libraryName>Waikato Library</libraryName>
<item>
[...]
</item>
</library>
</loan>
The data is to be displayed as an html table, sorted by Library, duedate and title (in that order). The XML data is not sorted. Overdue books are highlighted:
Auckland Library Items | Due
|
---|
Hide Your Heart | 12 May
| Empire Burlesque | 24 May
| Waikato Library Items | Due
|
---|
... | ...
|
I've only had limited success so far and its giving me a headache. Can anyone suggest the best way to do this? Thanks.
|
|
|
|
|
Steps.
1.Learn the basics of C#
2. Figure out what the Html needs to look like. That has nothing to do with XML.
3. Figure out how to parse (step through) the XML. That has nothing to do with Html.
4. Put 2 and 3 together.
Ask questions about each of the specific tasks.
|
|
|
|
|
|
In my experience it is more like....
5. Find good sales person.
6. Profit!
Regardless of steps before 5, you will not get to 6 without 5.
|
|
|
|
|
|
Another example of transforming XML file to HTML table
take a look there[^].
This link[^] also might help you.
|
|
|
|
|
How to sending fax with C# 2010 in windows 7?
tnx
modified on Friday, May 13, 2011 9:14 AM
|
|
|
|
|
Type faxcomexlib in google. I am sure you will find a good point where you can start from.
|
|
|
|
|
FAXCOMEXLib is indeed necessary here, but beware - it's known to cause a lot of problems. Luckily for you, I've come across this list of common FAXCOMEXLib errors. This might save you a lot of time and effort which were, needless to say, doggedly spent when I had a similar problem
|
|
|
|
|
Thanks... but I guess that would be more useful to the op, and maybe he will not see your post becouse you have answered to me.
|
|
|
|
|
i'm trying to get information about a panel in my application , so i called GetWindowLong passing this.handle in the custom panel class, but i get the error 6 saying invalid handle, this.handle returns "2081229920" value.
any idea,
thanx in advance.
|
|
|
|
|
Every mature Winforms Control has a Handle property, that is what you should use as a HWND when P/Invoking API functions; and the Handle typically only exists when the Control has been fully created, which I think is when the holding Form is Loaded or Shown.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
thanx, ok actually i made that call in the onpaint method, but when i debug this.handle returned a value. so even though the handle is there it is not enough , is it the reason , means the control is not completely drawn.
thanx
|
|
|
|
|
if you want more help, you'll have to provide a better description of the situation and the problem, and probably show some code. I also am puzzled why you would need handles in an OnPaint handler, I've never seen that. I do know it is wise to keep paint handlers short, simple and swift.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
You got A value, not a VALID value. Having a value other than zero does not means it's valid.
You can check this with the IsHandleCreated property of your control.
|
|
|
|
|
Hi
I seem to be having a problem matching names using Regex, even though it works perfectly in Expresso.
The goal is to match only the name in the string below.
Here is the Regex I`m using:
\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b
..and here is the string I`m matching:
"JOHN DOE john@random.net(H) 04377777746, (W) 0444444543, (F) 022222223, (M) 082343222;"
Like I said, in Expresso it matches the full name, but when I use:
MatchCollection NameCollectionRegex = Regex.Matches(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)");
or
Match m = Regex.Match(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)");
it doesn't return any matches. Anything I could be doing wrong?
|
|
|
|
|
Are you missing parentheses around your character class for upper and lower-case letters? I think [a-z][A-Z]* in both places should be replaced with ([a-z][A-Z])+.
P.S. There is a separate forum for regular expressions[^].
|
|
|
|
|
Firstly, we do have a Regex forum: http://www.codeproject.com/Forums/1580841/Regular-Expressions.aspx[^] - it might be more appropriate in future.
Secondly, in Expresso, go to the Design tab and look at the bottom: You will find you have "Ignore case" selected.
If you de-select that, Expresso will not find it either.
Change your string to:
\b[a-zA-Z][a-zA-Z]*\b\s*\b[a-zA-Z][a-zA-Z]*\b And it will work in both, or use the RegexOption.IgnoreCase as part of the Match method. MSDN[^]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
Thanks I only noticed that now.
|
|
|
|
|
Griff's answer seems appropriate for the question you asked, however your question may be a bit shortsighted, as names can be a bit more complex than you are expecting.
Here are a few CP member names you would still have trouble with:
wout de zeeuw
Pete O'Hanlon
Smithers-Jones
Ennis Ray Lynch, Jr.
What I would do is locate the first digit or @ sign, then get everything that sits before the last space in front of that. And no, I would not use regex to implement that, it is way simpler using two methods from the string class.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Luc Pattyn wrote: What I would do is locate the first digit ...
Perhaps you aren't familiar with Tom Lehrer's friend Hen3ry?
|
|
|
|