Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Networking TcpClient
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 ??
 
Thanks.
N.A.S
 
Client side:
private void button1_Click(object sender, EventArgs e)
       {
           client = new TcpClient();
           client.Connect("127.0.0.1", 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("127.0.0.1");
            TcpListener ls = new TcpListener(localAddr,port); 
            ls.Start();
            TcpClient clientOne = ls.AcceptTcpClient();
           byte[] b = new byte[1024]; 
            while (true)
            {
                clientOne.GetStream().Read(b, 0, 1024);
                //MessageBox.Show(Encoding.Unicode.GetString(b));
                textBox1.Text = Encoding.Unicode.GetString(b);
            }
            
 
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            new Thread(new ThreadStart(rcv)).Start();}
 

Posted 22-Jan-13 2:50am
Comments
Andreas Gieriet at 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.
Andi
nada2006 at 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 at 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?
Andi
nada2006 at 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 at 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!
Andi
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 
Cheers
Andi
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2014
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