当前位置: 技术问答>linux和unix
linux 下g++编译 书上的socket的例子出错,请帮忙看看?
来源: 互联网 发布时间:2015-11-30
本文导语: 我客户端例子: #include #include #include #include #include #include #include #include #define PORT 3490 #define MAXDATASIZE 100 int main( int argc, char *argv[] ) { int sockfd, numbytes; char buf[MAXDATASIZE]; struct hostent *he...
我客户端例子:
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 3490
#define MAXDATASIZE 100
int main( int argc, char *argv[] )
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr;
if( 2 != argc )
{
fprintf( stderr, "usage:clent hostname" );
exit( 1 );
}
if(( he = gethostbyname( argv[1] )) == NULL )
{
perror( "gethostbyname" );
exit( 1 );
}
if( ( sockfd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 )
{
perror( "socket" );
exit( 1 );
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons( PORT );
their_addr.sin_addr = *((struct in_addr*)he->h_addr );
bzero( &(their_addr.sin_zero), 8 );
if( connect( sockfd, (void*)&their_addr, sizeof( their_addr ) ) == -1 )
{
perror( "connect" );
exit( 1 );
}
if( ( numbytes = recv( sockfd, buf, MAXDATASIZE, 0 ) ) == -1 )
{
perror( "recv" );
exit( 1 );
}
buf[numbytes] =0 ;
printf( "received: %s", buf );
close( sockfd );
return 0;
}
用gcc 编译没有问题
[gliusheng@liushenglinux Client]$ make
gcc -g -c -o TcpCli.o TcpCli.c
gcc -o TcpCli TcpCli.o
[gliusheng@liushenglinux Client]$
用g++编译问题如下:
[gliusheng@liushenglinux Client]$ make
g++ -g -c -o TcpCli.o TcpCli.c
TcpCli.c: In function `int main(int, char**)':
TcpCli.c:36: invalid conversion from `void*' to `const sockaddr*'
TcpCli.c:48: `close' undeclared (first use this function)
TcpCli.c:48: (Each undeclared identifier is reported only once for each
function it appears in.)
make: *** [TcpCli.o] Error 1
[gliusheng@liushenglinux Client]$
刚学Linux不久,请指点。多谢。
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 3490
#define MAXDATASIZE 100
int main( int argc, char *argv[] )
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr;
if( 2 != argc )
{
fprintf( stderr, "usage:clent hostname" );
exit( 1 );
}
if(( he = gethostbyname( argv[1] )) == NULL )
{
perror( "gethostbyname" );
exit( 1 );
}
if( ( sockfd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 )
{
perror( "socket" );
exit( 1 );
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons( PORT );
their_addr.sin_addr = *((struct in_addr*)he->h_addr );
bzero( &(their_addr.sin_zero), 8 );
if( connect( sockfd, (void*)&their_addr, sizeof( their_addr ) ) == -1 )
{
perror( "connect" );
exit( 1 );
}
if( ( numbytes = recv( sockfd, buf, MAXDATASIZE, 0 ) ) == -1 )
{
perror( "recv" );
exit( 1 );
}
buf[numbytes] =0 ;
printf( "received: %s", buf );
close( sockfd );
return 0;
}
用gcc 编译没有问题
[gliusheng@liushenglinux Client]$ make
gcc -g -c -o TcpCli.o TcpCli.c
gcc -o TcpCli TcpCli.o
[gliusheng@liushenglinux Client]$
用g++编译问题如下:
[gliusheng@liushenglinux Client]$ make
g++ -g -c -o TcpCli.o TcpCli.c
TcpCli.c: In function `int main(int, char**)':
TcpCli.c:36: invalid conversion from `void*' to `const sockaddr*'
TcpCli.c:48: `close' undeclared (first use this function)
TcpCli.c:48: (Each undeclared identifier is reported only once for each
function it appears in.)
make: *** [TcpCli.o] Error 1
[gliusheng@liushenglinux Client]$
刚学Linux不久,请指点。多谢。
|
楼上大雪说了
c的代码干么要用g++编译啊?想不明白
c的代码干么要用g++编译啊?想不明白
|
并不是所有c程序都能用g++编译的
c++的类型检查更严格,如果一定要用c++,你得修改代码。不过也许g++有参数可以兼容
c++的类型检查更严格,如果一定要用c++,你得修改代码。不过也许g++有参数可以兼容
|
强制转化以下
close要加unistd.h
close要加unistd.h