日期:2014-05-16  浏览次数:20719 次

socket 编程 connect ()的问题求教!
本人在AIX写了段SOCKET   SERVER程序,运行之后,察看了端口状态为LISTEN,server端程序也运行到了accept()了,但client死活都连不上去,提示“Connection   refused”,用telnet也测试了服务端口,同样是“Connection   refused”,有哪位高手碰到过类似的这种情况吗?告诉我一下解决的办法,不胜感激!被折磨的快崩溃了......   帮帮我!

------解决方案--------------------
自己对吧。。。


/*
* A test for anaylise the HTTP
* @author Mike
*/

#include <iostream>
#include <cerrno>
#include <cstdlib>
#include <unistd.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define PORT 8081
#define MAXMSG 512
#define MAXQUE 3

using namespace std;

int sock = 0;

void* DoResponse(void* data)
{
char* buffer = new char[MAXMSG];
int nbyte = MAXMSG;
int client = int(data);
cout < < "============================================== " < < endl;
do
{
nbyte = read(client, buffer, MAXMSG);
cout < < buffer;
}while(nbyte > 0);
delete buffer;
}

void CreateThread(int client)
{
pthread_t pid;
int ret = pthread_create(&pid, NULL, &DoResponse, (void*)client);
if(ret != 0)
{
cout < < "Thread Creation Error\n ";
return;
}
}

int main(int argc,char* argv[])
{
sockaddr_in name;
sockaddr_in clientname;

sock = socket(PF_INET, SOCK_STREAM, 0);
if(!sock)
cout < < "Error when Creating Server Socket.\n ";

name.sin_family = AF_INET;
name.sin_port = htons(PORT);
name.sin_addr.s_addr = htonl(INADDR_ANY);

if(bind(sock, (const sockaddr*)&name, sizeof(name)) == -1)
cout < < "Error when Binding Server Socket.\n ";

if(listen(sock, MAXQUE) == -1)
cout < < "Error when Listening.\n ";

while(true)
{
fd_set fd;
FD_ZERO(&fd);
FD_SET(sock, &fd);
if(select(FD_SETSIZE, &fd, NULL, NULL, NULL) == -1)
cout < < "Selecting Error\n ";

socklen_t size;
int client_sock = accept(sock, (sockaddr*)&clientname, &size);
if(client_sock == -1)
{
cout < < "Error when Accepting.\n ";
continue;
}
CreateThread(client_sock);
}

cout < < "Server Exiting...\n ";

return 0;
}

------解决方案--------------------
首先确认firewall没有启动,然后在client端抓包分析是否握手的数据正确的