title: Socket programming between client in C#.NET and server in C
body: <p>Here I would like to ask how to create communication through socket programming between client  (on Windows, using C#.NET) and server (on Ubuntu, using C).
  I have done successfully if the server-client both are either C#.NET or C but have no idea to do as I asked.
  I have tried to find information in several sites but couldn't find the exact one. 
  I appreciate any comments. Thank you.</p>
  
  <p>Here is simple codes on client.</p>
  
  <pre><code>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;
  using System.Net.Sockets;
  
  namespace WindowsFormsApplication9
  {
      public partial class Form1 : Form
      {
          // Receiving byte array  
          byte[] bytes = new byte[1024];
          Socket senderSock;
  
          public Form1()
          {
              InitializeComponent();
  
          }
  
  
  
          private void Form1_Load(object sender, EventArgs e)
          {
  
          }
  
          private void btn_connect_Click(object sender, EventArgs e)
          {
              try
              {
                  // Create one SocketPermission for socket access restrictions 
                      SocketPermission permission = new SocketPermission(
                      NetworkAccess.Connect,    // Connection permission 
                      TransportType.Tcp,        // Defines transport types 
                      "",                       // Gets the IP addresses 
                      SocketPermission.AllPorts // All ports 
                      );
  
                  // Ensures the code to have permission to access a Socket 
                  permission.Demand();
  
                  // Resolves a host name to an IPHostEntry instance            
                  IPHostEntry ipHost = Dns.GetHostEntry("");
  
                  // Gets first IP address associated with a localhost 
                  IPAddress ipAddr = ipHost.AddressList[0];
  
                  // Creates a network endpoint 
                  IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 4510);
  
                  // Create one Socket object to setup Tcp connection 
                  senderSock = new Socket(
                      ipAddr.AddressFamily,// Specifies the addressing scheme 
                      SocketType.Stream,   // The type of socket  
                      ProtocolType.Tcp     // Specifies the protocols  
                      );
  
                  senderSock.NoDelay = false;   // Using the Nagle algorithm 
  
                  // Establishes a connection to a remote host 
                  senderSock.Connect(ipEndPoint);
  
  
  
              }
              catch (Exception exc) { MessageBox.Show(exc.ToString()); }
  
          }
  
          private void btnSend_Click(object sender, EventArgs e)
          {
              try
              {
                  // Sending message 
                  //&lt;Client Quit&gt; is the sign for end of data 
                  string theMessageToSend = text_IP.Text;
                  byte[] msg = Encoding.Unicode.GetBytes(theMessageToSend + "&lt;Client Quit&gt;");
  
                  // Sends data to a connected Socket. 
                  int bytesSend = senderSock.Send(msg);
  
  
              }
              catch (Exception exc) { MessageBox.Show(exc.ToString()); }
          }
  
  
  
      }
  }
  </code></pre>
  
  <p>this is the codes in server (in C)</p>
  
  <pre><code>include "stdio.h"
  #include "stdlib.h"
  #include "sys/socket.h"
  #include "sys/types.h"
  #include "netinet/in.h"
  #include "error.h"
  #include "strings.h"
  #include "unistd.h"
  #include "arpa/inet.h"
  
  #define ERROR    -1
  #define MAX_CLIENTS    
  #define MAX_DATA    1024
  
  
  main(int argc, char **argv)
  {
      struct sockaddr_in server;
      struct sockaddr_in client;
      int sock;
      int new,i;
      int sockaddr_len = sizeof(struct sockaddr_in);
      printf("sockaddr_len");
      int data_len;
      char data[MAX_DATA];
      char temp[MAX_DATA];
  
  
      if((sock = socket(AF_INET, SOCK_STREAM, 0)) == ERROR)
      {
          perror("server socket: ");
          exit(-1);
      }
  
      server.sin_family = AF_INET;
      server.sin_port = htons(atoi(argv[1]));
      server.sin_addr.s_addr = INADDR_ANY;
      bzero(&amp;server.sin_zero, 8);
  
      if((bind(sock, (struct sockaddr *)&amp;server, sockaddr_len)) == ERROR)
      {
          perror("bind : ");
          exit(-1);
      }
  
      if((listen(sock, MAX_CLIENTS)) == ERROR)
      {
          perror("listen");
          exit(-1);
      }
      printf("\nThe TCPServer Waiting for client on port %d\n",ntohs(server.sin_port));
          fflush(stdout);
  
      while(1) // Better signal handling required
      {
          if((new = accept(sock, (struct sockaddr *)&amp;client, &amp;sockaddr_len)) == ERROR)
          {
              perror("accept");
              exit(-1);
          }
  
          printf("New Client connected from port no %d and IP %s\n", ntohs(client.sin_port), inet_ntoa(client.sin_addr));
  
          data_len = 1;
  
  
          while(data_len)
          {
              data_len = recv(new, data, MAX_DATA, 0);
              printf("\nRecieved mesg from client: %s", data);
  
              for( i = 0; data[ i ]; i++)
              {
                  /* if(data[i]=='a' || data[i]=='e' || data[i]=='i' ||data[i]=='o' || data[i]=='u' )
                      data[ i ] = toupper( data[ i ] );
                  else */
                  data[ i ] = data[ i ] ;
  
              }    
  
              if(data_len)
              {
  
                  send(new, data, data_len, 0);
                  data[data_len] = '\0';
                  printf("\nSent mesg to client: %s", data);
              }
  
          }
  
          printf("Client disconnected\n");
  
          close(new);
  
      }
  
      close(sock);
  
  
  }
  </code></pre>