10

I am testing this locally, so the IP to connect to can be localhost or 127.0.0.1

After sending it, it receives a string back. That would also be handy.

5
  • 7
    What did you try, what didn't work? Show us you've done some research on the topic. Commented Jan 7, 2012 at 22:42
  • Right, of course. I looked up some stuff on the Microsoft website, it gave me quite a code block. And in theory, it should work but my server side tells me nothing connected. (Which is made in Java, but that shouldn't matter, right?) This is what my code is: pastebin.com/ANy7Jcu2 Commented Jan 7, 2012 at 22:43
  • Just read this, it was the first google link : developerfusion.com/article/3918/socket-programming-in-c-part-1 Commented Jan 7, 2012 at 22:46
  • It's great they added source to there, looking at it now. I did find it before, but it gave some nasty errors. Didn't see the source link though :) Commented Jan 7, 2012 at 22:54
  • It's working now, I'll post the answer in a bit. Commented Jan 8, 2012 at 11:07

2 Answers 2

23
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse(server);
System.Net.IPEndPoint remoteEP = new IPEndPoint(ipAdd, 3456);
soc.Connect(remoteEP);

For connecting to it. To send something:

//Start sending stuf..
byte[] byData = System.Text.Encoding.ASCII.GetBytes("un:" + username + ";pw:" + password);
soc.Send(byData);

And for reading back..

byte[] buffer = new byte[1024];
int iRx = soc.Receive(buffer);
char[] chars = new char[iRx];

System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
System.String recv = new System.String(chars);
Sign up to request clarification or add additional context in comments.

3 Comments

Just be aware that there are no delimiters in sockets so sending 0123456789 might result in having to read once, twice, thrice... ie reading 0123 followed by 456789 one day and 01234567 with 89 another, and 0123456789 on a different attempt with a single read. The larger the data the more likely it is to be split upon reading.
Would there be any reason why soc.Connect(remoteEP); would fail to connect on 127.0.0.1 with No connection could be made because the target machine actively refused it?
The only reason for a message of actively refused it would be that the socket is not open for listening or that a software firewall might be blocking it
2
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipAdd = System.Net.IPAddress.Parse(m_ip);
IPEndPoint remoteEP = new IPEndPoint(ipAdd, m_port);

socket.Connect(remoteEP);

byte[] byData = System.Text.Encoding.ASCII.GetBytes("Message");
socket.Send(byData);


socket.Disconnect(false);
socket.Close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.