473,659 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linux socket help

As you can tell by my code that I will post I am obviously new with linux
socket programming so to be to hard on me :)

When I run my little program I get this error:
Server: got connection from 192.168.0.5
recv: Transport endpoint is not connected.

I don't know what that means or why I am getting it.
I know the connection is made by the server: message.

My other end program is a VB program. When i uncomment out the appropriate
lines in the following code my VB app works just find for receiving
messages.

Thanks

Expand|Select|Wrap|Line Numbers
  1.  
  2. /*
  3. ** server.c -- a stream socket server demo
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #include <arpa/inet.h>
  15. #include <sys/wait.h>
  16. #include <signal.h>
  17. #include <string.h>
  18.  
  19. #define MYPORT 3490
  20.  
  21. #define BACKLOG 10
  22.  
  23. #define MAXDATASIZE 100
  24.  
  25. void sigchld_handler(int s)
  26. {
  27. while(wait(NULL) > 0);
  28. }
  29.  
  30. int main(void)
  31. {
  32. int sockfd, new_fd;
  33. struct sockaddr_in my_addr;
  34. struct sockaddr_in their_addr;
  35. int sin_size;
  36. struct sigaction sa;
  37. int yes=1;
  38. char msg[200]={""};
  39. int msgLen;
  40. int numbytes;
  41. char buf[MAXDATASIZE];
  42.  
  43.  
  44.  
  45. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
  46. perror("socket");
  47. exit(1);
  48. }
  49.  
  50. if
  51. (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int))== -1) {
  52. perror("setsockopt");
  53. exit(1);
  54. }
  55.  
  56. my_addr.sin_family = AF_INET;
  57. my_addr.sin_port = htons(MYPORT);
  58. my_addr.sin_addr.s_addr = INADDR_ANY;
  59. memset(&(my_addr.sin_zero), '\0', 8);
  60.  
  61. if (bind(sockfd, (struct sockaddr *)&my_addr,sizeof(struct
  62. sockaddr)== -1)
  63. {
  64. perror("bind");
  65. exit(1);
  66. }
  67.  
  68. if (listen(sockfd, BACKLOG) == -1) {
  69. perror("listen");
  70. exit(1);
  71. }
  72.  
  73. sa.sa_handler = sigchld_handler;
  74. sigemptyset(&sa.sa_mask);
  75. sa.sa_flags = SA_RESTART;
  76. if (sigaction(SIGCHLD, &sa, NULL) == -1) {
  77. perror("sigaction");
  78. exit(1);
  79. }
  80. sin_size = sizeof(struct sockaddr_in);
  81. if ((new_fd = accept(sockfd, (structsockaddr *)&their_addr,
  82. &sin_size)) == -1)
  83. {
  84. perror("accept");
  85. }
  86. printf("server: got connection
  87. from%s\n",inet_ntoa(their_addr.sin_addr));
  88. while(1)
  89. {
  90. //    This code works great sending a message to my other VB
  91. app
  92. //*********************************************
  93. //printf("Enter a message to sent: ");
  94. //gets(msg);
  95. //msgLen=strlen(msg);
  96. //   if (send(new_fd, msg, msgLen, 0) ==-1)
  97. //       perror("send");
  98. //********************************************
  99.  
  100. //this is the code that doesnt work
  101. //*********************************************************************
  102.  
  103. if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) ==-1)
  104. {
  105. perror("recv");
  106. exit(1);
  107. }
  108.  
  109. buf[numbytes] = '\0';
  110.  
  111. printf("Received: %s\n",buf);
  112.  
  113. }
  114. //************************************************************************
  115. return 0;
  116. }
  117.  
  118.  
  119.  

Nov 14 '05 #1
3 5996
Robert Smith wrote:

As you can tell by my code that I will post I am obviously new
with linux socket programming so to be to hard on me :)

When I run my little program I get this error:
Server: got connection from 192.168.0.5
recv: Transport endpoint is not connected.

I don't know what that means or why I am getting it.
I know the connection is made by the server: message.

My other end program is a VB program. When i uncomment out the
appropriate lines in the following code my VB app works just
find for receiving messages.

/*
** server.c -- a stream socket server demo
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Not a standard include file
#include <errno.h>
#include <string.h>
#include <sys/types.h>
Not a standard include file
#include <sys/socket.h>
Not a standard include file
#include <netinet/in.h>
Not a standard include file
#include <arpa/inet.h>
Not a standard include file
#include <sys/wait.h>
Not a standard include file
#include <signal.h>
#include <string.h>


With all those errors we cannot possibly understand your
non-portable code here. Try a newsgroup that deals with your
actual system.

--
"I'm a war president. I make decisions here in the Oval Office
in foreign policy matters with war on my mind." - Bush.
"If I knew then what I know today, I would still have invaded
Iraq. It was the right decision" - G.W. Bush, 2004-08-02
Nov 14 '05 #2
Ash
> if (bind(sockfd, (struct sockaddr *)&my_addr,size of(struct
sockaddr)== -1)
{
perror("bind");
exit(1);
}


Here the bind arguments are not proper. The third argument should
contain the size of the address structure whichis sizeof(my_addr) .
Nov 14 '05 #3
On Sun, 05 Sep 2004 16:12:03 GMT, "Robert Smith" <no**@none.co m>
wrote:
As you can tell by my code that I will post I am obviously new with linux
socket programming so to be to hard on me :)
Sockets, and (other) Linux specifics, are offtopic in clc, which deals
with standard/portable C. Try comp.unix.progr amming or (I think it is)
comp.os.linux.d evelopment.apps . But:
<snip> if ((new_fd = accept(sockfd, (structsockaddr *)&their_addr,
&sin_size)) == -1) <snip> // if (send(new_fd, msg, msgLen, 0) ==-1) <snip> if ((numbytes=recv (sockfd, buf, MAXDATASIZE-1, 0)) ==-1)


Think about which fd is which.

- David.Thompson1 at worldnet.att.ne t
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4974
by: Tom Jones | last post by:
I need to access databases on both win2k and Linux systems but I have to set up the DSNs under program control ... no uses using GUIs ... Has anyone done this? if so can we discuss it either in here or offline? thanks Tom
0
3633
by: Google Mike | last post by:
After a lot of thought and research, and playing with FreeTDS and InlineTDS, as well as various ODBC connections, I have determined that the fastest and cheapest way to get up and going with PHP on Linux, connecting to MS SQL Server, unless it was already pre-installed by your Linux installation, is to build your own multithreaded TCP socket server on Windows and connect to it through the socket API in PHP on Linux (if you have installed...
7
17943
by: gurtd_dauberie | last post by:
I can't get the correct IP address on Linux... The following code works properly on Windows and Unix. But on Linux, I always get the Loopback Address (127.0.0.1) Here is the code : //gets the IP via InetAddress class //works on UNIX and WIN, but not on LINUX !! InetAddress hostIP = InetAddress.getLocalHost(); String hostIPStr = hostIP.getHostAddress();
2
2576
by: xunil | last post by:
hi, could any one please help show me how to run a python script in linux ? when i try "python script.py" it just does nothing this is the script iam trying to run #!/usr/bin/env python "USAGE: echoserver.py <port>"
9
20889
by: 8anos | last post by:
Hello, I am new at the community and newbie at programming :) As you may know rapidshare provides a perl script for linux, to upload files at their servers. You can find the original scripts at rapidshare news : http://images.rapidshare.com/software/rsapi.pl If you test it you will see that you can upload one file at time. I try to modify it in that way that script can read a text file with the names of the files i want to...
5
2795
by: ganeshp | last post by:
Hi , In Java using socket programming is it possible to have a server program on windows that services a client program on linux? I tried the code in the below given link: http://williams.comp.ncat.edu/Networks/JavaSocketExample.htm This does not work when the client program is on a Linux machine. Error: Error Connection refused
1
4271
by: getafixx | last post by:
Hello everyone, We have a linux server (Fedora core 7, default install, firewall turned off) and a bunch of windows XP machines on network/domain. All machines are visible and I can get to both windows and linux by various methods. We are trying to resolve a problem of not being able to connect to the linux box from the xp machines using python XMLRPC. (This is the module used in the software I am trying to set up) Python is at 2.4...
3
11660
by: TsanChung | last post by:
I want to make a java TCP socket client to communicate with a TCP server socket on linux. Are there some sample C unix server and java client socket programs available? The Richard Stevens' "Unix network programming" book described a TCP server (tcpcliserv04.c) and client (tcpcli04.c). I compiled and executed them successfully as follows: $ tcpcliserv04 &
4
5500
by: The Doctor | last post by:
Hey people, I have two applications: the server, which creates a server socket, waits for a real-time signal, and if it receives one, it creates a client socket. The client, which will connect to the server. This will generate a real- time signal at the server, who will create a client socket for this client.
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8535
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8629
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2757
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.