Click here to Skip to main content
12,634,407 members (25,173 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# Network TCP/IP
hello

I have 2 program , the first "server" and the second is the client

the client connect to server and send message
but the server can't connect to client and send message :(
what the problem ?
how can the server connect to client and sent message ?

I use this code in server

             TcpClient tc = new TcpClient();
             tc.Connect(ip, int.Parse("5050"));


             SendMsg(tc, "hello");


what is the wrong ? please help :(
thanks
Posted 1-Jun-12 10:52am
Comments
Oleksandr Kulchytskyi 1-Jun-12 16:12pm
   
Please, provide us with your internal implementation of SendMsg method.
maxpower12345 1-Jun-12 16:32pm
   
public void SendMsg(TcpClient myclient, string msg)
{

NetworkStream clientStream = myclient.GetStream();
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes(msg);


clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();

}
SoMad 1-Jun-12 16:39pm
   
For normal Client/Server development, the server has a listening socket waiting for clients to connect. The clients initiates the connection and messages are exchanged.
Have you made an implementation where both the client and the server have listening sockets? If that is the case, you might have good reasons for that, just make sure both of them don't listen on the same port.

Soren Madsen
maxpower12345 1-Jun-12 16:52pm
   
its different port
its just give me this error
No connection could be made because the target machine actively refused it 127.0.1.6:5050
SAKryukov 1-Jun-12 16:58pm
   
What makes you thinking that is should accept the connection? What do you run on the other side? Do you in fact know that?
--SA
Richard MacCutchan 2-Jun-12 4:59am
   
That IP address does not look right. 127 addresses refer to the local machine.
SAKryukov 1-Jun-12 16:57pm
   
What's wrong? Nothing is right. Please pay attention for the comment by SoMad. Probably, your idea on how it's working has little to do with reality...
--SA
maxpower12345 1-Jun-12 17:45pm
   
I dont understand :(
what is the wrong ?
where is my mistake ? if they don't listen to the same port , so what is the wrong?
:(

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hello

Look at this:

Sharing data among Windows Forms.
Especially this part: "Is there a way to send a message back to the server from the client?"
  Permalink  
v2
Comments
VJ Reddy 4-Jun-12 19:51pm
   
Good reference 5!
Shahin Khorshidnia 5-Jun-12 0:42am
   
Thank you VJ :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161208.2 | Last Updated 3 Jun 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100