Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
C#
TcpClient tc = new TcpClient();
tc.Connect(ip, int.Parse("5050"));


SendMsg(tc, "hello");


what is the wrong ? please help :(
thanks
Posted
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
Sergey Alexandrovich Kryukov 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

1 solution

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?"
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900