Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Client and Server Program

Client and Server Program

Scheduled Pinned Locked Moved C#
16 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M musefan

    you do know that error messages are not just random characters? mostly they tell you the problem and nearly always you can throw them into google and get an answer that will fix your problem in minutes.. both apply in this case

    Life goes very fast. Tomorrow, today is already yesterday.

    V Offline
    V Offline
    Vinziee
    wrote on last edited by
    #5

    hi musefan i tried googling but cudnt find proper ans if u cud help me with the coding then i wud be really grateful to you thx in advance

    L 1 Reply Last reply
    0
    • V Vinziee

      hi SeMartens first of all thx for replyin but frankly speakin i checked out ur article but cudnt catch a word can u explain me in simpler words plzzzz.... .. help me with the solution

      S Offline
      S Offline
      SeMartens
      wrote on last edited by
      #6

      Okay, let me try. Your server opens a socket at port 8080 with

      byte[] recieved = server.Receive(ref ip);

      This socket will be open all the time your program is running. You have to close this socket when your application exits. So you have to use following instruction:

      server.Close()

      Regards Sebastian

      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

      V 2 Replies Last reply
      0
      • V Vinziee

        hi musefan i tried googling but cudnt find proper ans if u cud help me with the coding then i wud be really grateful to you thx in advance

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #7

        Text speak dictionary... 'u'='you' 'cud'='could' 'cudnt'='couldn't or couldnt' 'thx'='thanks' 'wud'='would' 'ans'='answer' and my personal favorite 'help me with the coding'='do it, send it to me for free, and I probably will stop trolling until I find something else I'm to lazy to research and fix'

        Check out the CodeProject forum Guidelines[^]

        1 Reply Last reply
        0
        • V Vinziee

          A hi to all programming gurus i am an amature in network programming i have written an simple program for making a client and server in c# the cleint sends a msg and the server displays it in the listbox(lbconnections) The Problem is that---- this application runs perfectly after being deployed and afterward when i again use it it crashes (port already bieng used error) plz help me with this plzzzzzzzzzzzzzzzzzzzzzzzz..............:confused::confused::confused: ********************************coding for server******************************************* using System; using System.Threading; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; namespace server { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void recieve() { try { UdpClient server = new UdpClient(8080); while (true) { IPEndPoint ip = new IPEndPoint(IPAddress.Any, 0); byte[] recieved = server.Receive(ref ip); string returndata = Encoding.ASCII.GetString(recieved); listBox1.Items.Add(ip.Address.ToString() + " : " + returndata.ToString()); } } catch (SocketException ex) { MessageBox.Show(ex.Message); } } private void Form1_Load(object sender, EventArgs e) { Thread server = new Thread(new ThreadStart(recieve)); server.Start(); } } } ***************************************CODING FOR CLIENT********************************************* using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; namespace ser_cli { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { UdpClient client = new UdpClient(); client.Connect(tbHost.Text, 8080); byte[] send = Encoding.ASCII.GetBytes("HELLO WORLD?"); client.Send(send, s

          A Offline
          A Offline
          agent00zelda
          wrote on last edited by
          #8

          I am new to C# so I've been browsing the MSDN libraries to see how to use certain classes. After reading your post, I looked for and found the description of the UdpClient class. http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.aspx There are examples on that site, but specifically you need to close your server connection, as the others were telling you. So, somewhere before the user closes the program, you have to call server.Close() I would recommend a finally block in your receive() method and closing the server there. But you would have to define server before the try block for this to work.

          V 1 Reply Last reply
          0
          • S SeMartens

            Okay, let me try. Your server opens a socket at port 8080 with

            byte[] recieved = server.Receive(ref ip);

            This socket will be open all the time your program is running. You have to close this socket when your application exits. So you have to use following instruction:

            server.Close()

            Regards Sebastian

            It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

            V Offline
            V Offline
            Vinziee
            wrote on last edited by
            #9

            hey sebastian thx for ur reply . i tried that it still says only one usage of each socket is allowed ************************the new code for server************************************************ using System; using System.Threading; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; namespace server { public partial class Form1 : Form { UdpClient server; Thread thdserver; public Form1() { InitializeComponent(); } public void recieve() { try { server = new UdpClient(8080); while (true) { IPEndPoint ip = new IPEndPoint(IPAddress.Any, 0); byte[] recieved = server.Receive(ref ip); string returndata = Encoding.ASCII.GetString(recieved); listBox1.Items.Add(ip.Address.ToString() + " : " + returndata.ToString()); } } catch (SocketException ex) { MessageBox.Show(ex.Message); } } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { //recieve(); thdserver = new Thread(new ThreadStart(recieve)); thdserver.Start(); } private void button2_Click(object sender, EventArgs e) { server.Close(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { server.Close(); } } } *********************the new code for client**************************************** using System; using System.Threading; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; namespace ser_cli { public partial class Form1 : Form { UdpClient client = new UdpClient(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Thread th = new Thread(new ThreadStart(hear)); th.Sta

            S 1 Reply Last reply
            0
            • A agent00zelda

              I am new to C# so I've been browsing the MSDN libraries to see how to use certain classes. After reading your post, I looked for and found the description of the UdpClient class. http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.aspx There are examples on that site, but specifically you need to close your server connection, as the others were telling you. So, somewhere before the user closes the program, you have to call server.Close() I would recommend a finally block in your receive() method and closing the server there. But you would have to define server before the try block for this to work.

              V Offline
              V Offline
              Vinziee
              wrote on last edited by
              #10

              thx Mia Logan the actual problem lies with the server itself my client sends data properly but when i comes to receving the server throws an ecxception "the port can be used only once" this exception i'll check out msdn documentation of UDPClient as well thx for the advice

              1 Reply Last reply
              0
              • V Vinziee

                hey sebastian thx for ur reply . i tried that it still says only one usage of each socket is allowed ************************the new code for server************************************************ using System; using System.Threading; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; namespace server { public partial class Form1 : Form { UdpClient server; Thread thdserver; public Form1() { InitializeComponent(); } public void recieve() { try { server = new UdpClient(8080); while (true) { IPEndPoint ip = new IPEndPoint(IPAddress.Any, 0); byte[] recieved = server.Receive(ref ip); string returndata = Encoding.ASCII.GetString(recieved); listBox1.Items.Add(ip.Address.ToString() + " : " + returndata.ToString()); } } catch (SocketException ex) { MessageBox.Show(ex.Message); } } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { //recieve(); thdserver = new Thread(new ThreadStart(recieve)); thdserver.Start(); } private void button2_Click(object sender, EventArgs e) { server.Close(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { server.Close(); } } } *********************the new code for client**************************************** using System; using System.Threading; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; namespace ser_cli { public partial class Form1 : Form { UdpClient client = new UdpClient(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Thread th = new Thread(new ThreadStart(hear)); th.Sta

                S Offline
                S Offline
                SeMartens
                wrote on last edited by
                #11

                Check that every instance of the app is closed before you start a new try. By the way, does something else is running at port 8080? Maybe Apache Tomcat or something similar? Could be that your app is in conflict with another one. Regards Sebastian

                It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                V 1 Reply Last reply
                0
                • S SeMartens

                  Check that every instance of the app is closed before you start a new try. By the way, does something else is running at port 8080? Maybe Apache Tomcat or something similar? Could be that your app is in conflict with another one. Regards Sebastian

                  It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                  V Offline
                  V Offline
                  Vinziee
                  wrote on last edited by
                  #12

                  so how am i supposed to know wether the port is already occupied by some other application and which other port can i use

                  0 S 2 Replies Last reply
                  0
                  • V Vinziee

                    so how am i supposed to know wether the port is already occupied by some other application and which other port can i use

                    0 Offline
                    0 Offline
                    0x3c0
                    wrote on last edited by
                    #13

                    There's a list of standard ports here, but that doesn't include ports opened by applications which don't follow standards. Your best bet is going with something unlikely, like 65535; alternatively anything over 49151 should work

                    V 1 Reply Last reply
                    0
                    • V Vinziee

                      so how am i supposed to know wether the port is already occupied by some other application and which other port can i use

                      S Offline
                      S Offline
                      SeMartens
                      wrote on last edited by
                      #14

                      On windows you can use the "netstat"-command. This will list all applications and the ports the occupy.

                      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                      1 Reply Last reply
                      0
                      • 0 0x3c0

                        There's a list of standard ports here, but that doesn't include ports opened by applications which don't follow standards. Your best bet is going with something unlikely, like 65535; alternatively anything over 49151 should work

                        V Offline
                        V Offline
                        Vinziee
                        wrote on last edited by
                        #15

                        thx i used port 8080 n its working perfectly fine thx for ur help but now there is another problem is it possible to use the same udpclient object for sending as well as recieving data i mean if i create a UDPcleint object like void send() { UDPClient client = new UDPClient(); client.Connect(server, 8080); cleint.send(nedata); } void recieve() { UDPClient client = new UDPClient(); IpEndpoint ip = new IPEndponit(server, 8080) client.Recieve(ref ip) .... } void main() { thread thd_send = new thread(new threadStart(send)); thread thd_send = new thread(new threadStart(recieve)); } }

                        1 Reply Last reply
                        0
                        • S SeMartens

                          Okay, let me try. Your server opens a socket at port 8080 with

                          byte[] recieved = server.Receive(ref ip);

                          This socket will be open all the time your program is running. You have to close this socket when your application exits. So you have to use following instruction:

                          server.Close()

                          Regards Sebastian

                          It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                          V Offline
                          V Offline
                          Vinziee
                          wrote on last edited by
                          #16

                          hey thx Sebastian ur soluion worked perfectly fine

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups