当前位置: 技术问答>linux和unix
关于网络编程,不能达到希望的结果,希望各位高手指正一下!
来源: 互联网 发布时间:2015-06-16
本文导语: 关于网络编程,不能达到希望的结果,希望各位高手指正一下! 以下是源程序: 客户端程序 #include #include #include #include #include #include #include #include #define PORT 5555 #define MESSAGE "Yow!!! Are we having fun yet?...
关于网络编程,不能达到希望的结果,希望各位高手指正一下!
以下是源程序:
客户端程序
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 5555
#define MESSAGE "Yow!!! Are we having fun yet?!?"
#define SERVERHOST "192.168.0.45"
void
init_sockaddr (struct sockaddr_in *name,
const char *hostname, unsigned short int port)
{
struct hostent *hostinfo;
name->sin_family = AF_INET;
name->sin_port = htons (port);
hostinfo = gethostbyname (SERVERHOST);
if (hostinfo == NULL)
{
fprintf (stderr, "Unknown host %s.n", hostname);
exit (EXIT_FAILURE);
}
name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
}
void
write_to_server (int filedes)
{
int nbytes;
nbytes = write (filedes, MESSAGE, strlen (MESSAGE) + 1);
puts("send start!");
if (nbytes
以下是源程序:
客户端程序
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 5555
#define MESSAGE "Yow!!! Are we having fun yet?!?"
#define SERVERHOST "192.168.0.45"
void
init_sockaddr (struct sockaddr_in *name,
const char *hostname, unsigned short int port)
{
struct hostent *hostinfo;
name->sin_family = AF_INET;
name->sin_port = htons (port);
hostinfo = gethostbyname (SERVERHOST);
if (hostinfo == NULL)
{
fprintf (stderr, "Unknown host %s.n", hostname);
exit (EXIT_FAILURE);
}
name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
}
void
write_to_server (int filedes)
{
int nbytes;
nbytes = write (filedes, MESSAGE, strlen (MESSAGE) + 1);
puts("send start!");
if (nbytes