Click here to Skip to main content
15,611,549 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi ,

The following code work perfectly in in the same computer using the loopback ip ,but cause a problem if we try between two computers . Any suggestions ??


Client side:
private void button1_Click(object sender, EventArgs e)
           client = new TcpClient();
           client.Connect("", 8080);
       TcpClient client;

       private void button2_Click(object sender, EventArgs e)
           byte[] b = Encoding.Unicode.GetBytes(textBox1.Text);
           client.GetStream().Write(b, 0, Encoding.Unicode.GetByteCount(textBox1.Text));}

Server side:
private void rcv()

                        Int32 port = 8080;
                        IPAddress localAddr = IPAddress.Parse("");
            TcpListener ls = new TcpListener(localAddr,port); 
            TcpClient clientOne = ls.AcceptTcpClient();
           byte[] b = new byte[1024]; 
            while (true)
                clientOne.GetStream().Read(b, 0, 1024);
                textBox1.Text = Encoding.Unicode.GetString(b);


        private void button1_Click(object sender, EventArgs e)
            new Thread(new ThreadStart(rcv)).Start();}
Andreas Gieriet 22-Jan-13 8:35am    
What is the problem? Any error message? Exception?
Please also tag properly with the language in question with version, the framework, the OS.
nada2006 22-Jan-13 9:31am    
Thanks,i'm using Windows forms applications

it says life "there is a problem ,and the program might stop ",something like that ..we connected two computers together via crossover .

Andreas Gieriet 22-Jan-13 9:51am    
Do you know the IP address of each machine? Use ipconfig to show what you have. Do you run DHCP or do you assign static IP address or do you run none of them (i.e. you have auto IP)?
Can you ping each other machine?
nada2006 22-Jan-13 11:37am    
The network is working very well!- I can access both computers ..
when I run the server program in one computer ,it says " there is a problem,windows will close the window"..

I made sure of the other computer use the same Ip assigned to the server!

I use static ips.
Andreas Gieriet 22-Jan-13 13:52pm    
You are asked if you want to debug the crashing server - say yes and check the exception that is thrown.
How about running the server in the debugger?
To the code:
- Why do you run the server as separate thread?
- The problem is maybe related to the buffer handling in the server: checkout Encoding.GetString(...)
you try to convert a partially filled buffer into some unicode string.

But really: why don't you use the debugger - it's invented for that purpose!

See the following two links for a server and a client example in C#:
- Server[^]
- Client[^]

In the examples above, the character encoding is assumed to be ASCII (7-bit ASCII), thus, each buffer can be decoded immediately. If you transport any potentially multi-byte data, you must first invent some buffering (e.g. read all data before decoding) and only decode what you properly can decode.

Share this answer
what IP Address do you use?If the client and server are in the same LAN,you can use LAN ip address,but if they are not in the same LAN,your server must use the only global IP address.172.16.x.x to 172.31.x.x,192.168.x.x,10.x.x.x,these IP addresses can only be used in LAN.If your route find the data packets you send,whose destinaton IP addresses are the previous ones,it will discard them.
Share this answer

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