473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Error compilation

3
Hello guys!
Well.... it`s my code (c++/linux)

Expand|Select|Wrap|Line Numbers
  1. #include <arpa/inet.h>  
  2. #include <string.h>             
  3. #include <stdio.h>              
  4. #include <errno.h>              
  5. #include <unistd.h>             
  6. #include <stdlib.h>    
  7. #include <pthread.h>
  8. #include <ncurses.h>
  9.  
  10.      void *ReadMsg(int fd);
  11.      void *WriteMsg(int fd);    
  12.  
  13. int main(int argc, char* argv[])
  14. {     
  15.     pthread_t thread;
  16.     int sockfd;
  17.     char sendline[1000],recvline[1000]; 
  18.     struct sockaddr_in addr;    
  19.     bzero(sendline,1000);
  20.     bzero(recvline,1000);    
  21.     if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
  22.         perror("socket"); 
  23.         exit( -1);
  24.     }   
  25.     memset(&addr, 0, sizeof(addr));
  26.     addr.sin_family = AF_INET;
  27.     addr.sin_port = htons(51000);   
  28.     if(connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  29.         perror("connect");
  30.         close(sockfd);
  31.         exit( -1);
  32.     }    
  33.      printf("Connect is done!\n");
  34.     //Создание потоков    
  35.     if (pthread_create(&thread, NULL, ReadMsg, (void*) sockfd) != 0) return EXIT_FAILURE;
  36.     if (pthread_create(&thread, NULL, WriteMsg, (void*) sockfd) != 0) return EXIT_FAILURE;   
  37.     while (1);
  38.     close(sockfd);
  39. }
  40.  void *ReadMsg(int fd) {
  41.     int n;
  42.     char recvline[1000];
  43.     while (1) {
  44.         n = read(fd,recvline, 999);
  45.         printf("<< %s", recvline);
  46.     }
  47. } /* ReadMsg */
  48.  void *WriteMsg(int fd)
  49. {
  50.     char sendline[1000];
  51.     int n;
  52.     while ( 1 ) {
  53.         fgets(sendline, 1000, stdin);
  54.         if( (n = write(fd, sendline, strlen(sendline)+1)) < 0) {
  55.             perror("Can\'t write\n");
  56.             close(fd);
  57.             exit(1);
  58.         }
  59.     }
  60. } /* WriteMsg */
  61.  
my problems:
client1.cpp: In function ‘int main(int, char**)’:
client1.cpp:35: error: invalid conversion from ‘void* (*)(int)’ to ‘void* (*)(void*)’
client1.cpp:35: error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’
client1.cpp:36: error: invalid conversion from ‘void* (*)(int)’ to ‘void* (*)(void*)’
client1.cpp:36: error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’


Please help me!
Feb 10 '10 #1
6 2905
Golum
3
Anybody will answer? It is very necessary!
Feb 10 '10 #2
Banfa
9,065 Expert Mod 8TB
Try "man pthread_create" at you command line and you will find that the third parameter should be a pointer to a function with the prototype

void* threadFunction(void* parameter);

not a pointer to a function with the prototype

void* threadFunction(int parameter);

as you have defined them. The 2 different function pointers are incompatible.

You need to change ReadMsg and WriteMsg.
Feb 10 '10 #3
Banfa
9,065 Expert Mod 8TB
Anybody will answer? It is very necessary!
Please note that this is an international forum and there is no guarantee that the person that knows the answre to your question is awake at the time you post it. This forums ettiquette didtates that you should wait at least 24 hours before badgering us with extra posts demanding an answer.
Feb 10 '10 #4
Golum
3
I`m so sorry...


Expand|Select|Wrap|Line Numbers
  1. #include <arpa/inet.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <pthread.h>
  8. #include <ncurses.h>
  9.  
  10. #include <string>
  11. #include <iostream>
  12.  
  13. void *ReadMsg(void* pfd);
  14. void *WriteMsg(void* pfd);
  15. volatile bool quit = false;
  16.  
  17. int main(int argc, char* argv[])
  18. {
  19.     pthread_t rthread;
  20.     pthread_t wthread;
  21.     int sockfd;
  22.     char sendline[1000],recvline[1000];
  23.     struct sockaddr_in addr;
  24.     bzero(sendline,1000);
  25.     bzero(recvline,1000);
  26.     if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
  27.         perror("socket");
  28.         exit( -1);
  29.     }
  30.     memset(&addr, 0, sizeof(addr));
  31.     addr.sin_family = AF_INET;
  32.     addr.sin_port = htons(51000);
  33.     if(connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  34.         perror("connect");
  35.         close(sockfd);
  36.         exit( -1);
  37.     }
  38.     printf("Connect is done!\n");
  39.     //Создание потоков
  40.     if (pthread_create(&rthread, NULL, ReadMsg, (void*) sockfd) != 0) return EXIT_FAILURE;
  41.     if (pthread_create(&wthread, NULL, WriteMsg, (void*) sockfd) != 0) return EXIT_FAILURE;
  42.  
  43.     std::string str;
  44.     while ( std::cin >> str ) {
  45.         if ( str == "quit" ) {
  46.             quit = true;
  47.             break;
  48.         }
  49.     }
  50.  
  51.     pthread_join(rthread, NULL);
  52.     pthread_join(wthread, NULL);
  53.  
  54.     close(sockfd);
  55.     return 0;
  56. }
  57. void *ReadMsg(void* pfd) {
  58.     int n;
  59.     int fd = * ((int* ) pfd);
  60.     char recvline[1000];
  61.     while (1) {
  62.         if ( quit ) return NULL;
  63.         n = read(fd,recvline, 999);
  64.         printf("<< %s", recvline);
  65.     }
  66. } /* ReadMsg */
  67. void *WriteMsg(void* pfd)
  68. {
  69.     char sendline[1000];
  70.     int n;
  71.     int fd = * ((int* ) pfd);
  72.     while ( 1 ) {
  73.         if ( quit ) return NULL;
  74.         fgets(sendline, 1000, stdin);
  75.         if( (n = write(fd, sendline, strlen(sendline)+1)) < 0) {
  76.             perror("Can\'t write\n");
  77.             close(fd);
  78.             exit(1);
  79.         }
  80.     }
  81. } /* WriteMsg */
  82.  
It is compiled well, but at start there is a segmentation error! What not so?
Feb 11 '10 #5
newb16
687 512MB
It's hard to say what (is) "not so" here - it exits after perror("connect"); Maybe it's just cygwin to blame, I don't know.
Feb 11 '10 #6
Banfa
9,065 Expert Mod 8TB
In lines 40 and 41 you pass (void*) sockfd, that is a socket file descriptor (int) cast to a void*.

At lines 59 and 71 you access the parameter as * ((int* ) pfd), that is you treat it as a pointer to an int. But it was never a pointer to an int it was an int so you dereference some low invalid address in memory. That I imagine is causing the segmentation error.

If you are going to cast an int to a void*, which is what I would do, then on the other side you need to do the reverse, cast a void* to an int. Not treat it as if its a valid pointer.
Feb 11 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Sune | last post by:
Hi, previously I used Eclipse CDT for compiling my files just to get started with C and leave C++ behind. Now it's time to get a little more serious so I've moved my files to a new workplace and...
10
by: Brian Conway | last post by:
I have no idea what is going on. I have a Login screen where someone types in their login information and this populates a datagrid based off of the login. Works great in debug and test through...
0
by: psilu | last post by:
Hi , I have created a virtual directory and copied my web services file to corresponding physical directory. But when i am trying to discover the webservice from virtual directory i am getting...
2
by: tshad | last post by:
This has been driving me crazy. I have been trying to get the error handling working on my system and can get parts of it working and others won't work at all. I found that you can't access...
2
by: Balasubramanian Ramanathan | last post by:
I am getting this error while precompiling a website for deployment. Errorstack information error ASPRUNTIME: Object '/c107cdc3_b65c_4b6c_9d8f_82fe5a457fdb/ctofbyvagqsbvrgsc7xzxa5o_9.rem' has...
0
by: roger23 | last post by:
I get this error C:\Program Files\MSBuild\Microsoft\WebDeployment\v8.0\Microsoft.WebDeployment.targets(526,9): error MSB6006: "aspnet_compiler.exe" exited with code 1. at the end of my build...
21
by: Mark Rae | last post by:
Hi, I have an ASP.NET 2 web application (not web site) project and am using a web deployment project to deploy to the testing, then the production server. All has been working well for months...
0
by: dankyy1 | last post by:
hi ,i have an asp.net project runs on local intranet ,i use global.asax's onerror section to catch errors.so i got a simpleauth service does not exists error from some network clients .Error details...
7
by: news.microsoft.com | last post by:
I have an asp.net 2.0 project that when I change the build configuration to release I get the following error: Command line error BC2014: the value 'None' is invalid for option 'debug'. If I...
3
by: Hill | last post by:
This is an simple map, just an exercise. Who can help me fix this compile issue?Thanks in advance. #include <string> #include <vector> #include <iostream> using std::vector; using std::string;...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.