Note that there are some explanatory texts on larger screens.

plurals
  1. POConnecting C socket client to a C# socket server
    primarykey
    data
    text
    <p>im new to both C and C# language and i would appreciate any help/feedback on the following issue.</p> <p>Basically, im trying to make a socket connection in different programming languages. the client side has been developed in C and the server side in C#. Both application works well only when i tested in the same programming language, for example, the client side works well if the server is in C. same applies the server side in C#. the error encounter whenever the client.c tries to connect is "Connection refused" when gets this line in the code</p> <pre><code>if (connect(sockfd,(struct sockaddr *) &amp;serv_addr,sizeof(serv_addr)) &lt; 0) error("ERROR connecting"); </code></pre> <p>here is the code for both socket ends.</p> <p>client.c</p> <pre><code>/* Client socket demo */ int sockfd, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; char hostname[1024]; //hostname[1023] = '\0'; struct hostent* h; gethostname(hostname, sizeof(hostname)); //printf("Hostname: %s\n", hostname); server = gethostbyname(hostname); //printf("h_name: %s\n", h-&gt;h_name); //portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd &lt; 0) error("ERROR opening socket"); //server = gethostbyname(argv[1]); if (server == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(0); } bzero((char *) &amp;serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; // only IPv4 memcpy(&amp;serv_addr.sin_addr, server-&gt;h_addr, server-&gt;h_length); // fixed part bcopy((char *)server-&gt;h_addr,(char *)&amp;serv_addr.sin_addr.s_addr,server-&gt;h_length); serv_addr.sin_port = htons(SERVERPORT); // htons((u_short)port); .sin_addr.S_un.S_addr = htonl(INADDR_ANY) //serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); if (connect(sockfd,(struct sockaddr *) &amp;serv_addr,sizeof(serv_addr)) &lt; 0) error("ERROR connecting"); printf("Please enter the message: "); bzero(buffer,256); fgets(buffer,255,stdin); n = write(sockfd,buffer,strlen(buffer)); if (n &lt; 0) error("ERROR writing to socket"); bzero(buffer,256); n = read(sockfd,buffer,255); if (n &lt; 0) error("ERROR reading from socket"); printf("here we are %s\n",buffer); close(sockfd); </code></pre> <p>server.cs</p> <pre><code>void ButtonStartListenClick(object sender, System.EventArgs e) { try { // Check the port value if(textBoxPort.Text == "") { MessageBox.Show("Please enter a Port Number"); return; } string portStr = textBoxPort.Text; int port = System.Convert.ToInt32(portStr); // Create the listening socket... m_mainSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); IPEndPoint ipLocal = new IPEndPoint (IPAddress.Any, port); // Bind to local IP Address... m_mainSocket.Bind( ipLocal ); // Start listening... m_mainSocket.Listen(4); // Create the call back for any client connections... m_mainSocket.BeginAccept(new AsyncCallback (OnClientConnect), null); UpdateControls(true); } catch(SocketException se) { MessageBox.Show ( se.Message ); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload