我已经尝试在后台运行我的服务器并在ps下显示,我可以看到我的服务器正在运行,但是当我尝试运行客户端时,正确的服务器应该写入客户端说“成功连接到服务器”但没有任何东西正在运行在那里输出。
我的服务器代码
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
#ifndef AF_LOCAL
#define AF_LOCAL AF_UNIX
#endif
#ifndef PF_LOCAL
#define PF_LOCAL PF_UNIX
#endif
using namespace std;
int main()
{
int serverFd;
int clientFd;
int serverLen;
int clientLen;
string message;
string serverSockAddrPtr;
struct sockaddr* serverSockAddressPnt;
struct sockaddr* clientSockAddressPnt;
struct sockaddr_un serverAddress;
struct sockaddr_un clientAddress;
cout << "" << endl;
cout << "Running server program 'css' ...... " << endl;
cout << "" << endl;
// SOCKET CREATION PART - SERVER
serverFd = socket (AF_LOCAL, SOCK_STREAM, 0);
/* Set domain type */
serverAddress.sun_family = AF_LOCAL;
/* Set name */
strcpy (serverAddress.sun_path, "CServer");
/* GET SIZE OF Server Addres */
serverLen = sizeof serverAddress;
/* GET SIZE OF Client Addres */
clientLen = sizeof clientAddress;
/* Get Server Sock Address Pointer*/
serverSockAddressPnt = (struct sockaddr *) &serverAddress;
/* Get Client Sock Address Pointer*/
clientSockAddressPnt = (struct sockaddr *) &clientAddress;
/* Create file */
bind (serverFd, serverSockAddressPnt , serverLen);
/* listen for connection */
listen (serverFd,5);
cout << "" << endl;
// SOCKET CREATION END - SERVER
while(1)
{
//accept client connection
clientFd = accept(serverFd, clientSockAddressPnt, (socklen_t*)&clientLen);
if(clientFd >= 0)
{
if(fork() == 0)
{
message="Successfully connected to the server";
write(clientFd,message.c_str(),strlen(message.c_str())+1);
close(clientFd);
exit(0);
}
else
close(clientFd);
}
}
return 0;
}
我的客户代码
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
#define DEFAULT_PROTOCOL 0
#ifndef AF_LOCAL
#define AF_LOCAL AF_UNIX
#endif
#ifndef PF_LOCAL
#define PF_LOCAL PF_UNIX
#endif
using namespace std;
/* READ LINE FUNCTION*/
int readLine (int fd,char* str)
{
int n;
do
{
n = read(fd,str,1);
}while(n>0&& *str++ != 0);
return(n>0);
}
/* READ LINE FUNCTION END*/
/* READ SERVER FUNCTION*/
void readServer (int fd)
{
char str[500];
while (readLine(fd,str))
{
printf("%s",str);
}
}
/* READ SERVER FUNCTION END*/
int main()
{
int clientFd,serverLen,result;
struct sockaddr_un serverAddress;
struct sockaddr* serverSockAddressPnt;
/* Get Server Sock Address Pointer*/
serverSockAddressPnt = (struct sockaddr *) &serverAddress;
/* GET SIZE OF Server Addres */
serverLen = sizeof serverAddress;
clientFd = socket (AF_LOCAL, SOCK_STREAM, DEFAULT_PROTOCOL);
serverAddress.sun_family = AF_LOCAL;
strcpy (serverAddress.sun_path,"CServer");
do
{
// a loop to keep trying till connected.
result=connect(clientFd,serverSockAddressPnt,serverLen);
if(result==-1)
{
//wait for 1 second to retry
sleep(1);
}
}while(result==-1);
readServer (clientFd);
close (clientFd);
exit(0);
return 0;
}
答案 0 :(得分:0)
客户端不知道要连接的服务器。
您执行struct sockaddr* serverSockAddressPnt;
但这只会将serverSockAddressPnt
定义为指向struct sockaddr
的指针。但应该初始化或分配要连接的服务器的IP地址。