線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:5637
推到 Plurk!
推到 Facebook!

使用socket 問題

尚未結案
piyg8
一般會員


發表:2
回覆:0
積分:0
註冊:2009-04-21

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-04-22 10:35:46 IP:122.146.xxx.xxx 訂閱
各位大大:
小弟目前再寫一個Tcp server/client, 也就是server可以當成client, client可以當成server的程式 , 但是目前遇到一個問題我的 client可以寫到server端也可以秀出來, 但是server就是無法寫到client端,當然也沒辦法秀,請各位大大指點一下小弟我吧. 下面我把我的code貼出來....希望有大大能指點迷津!!
server.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 2345
#define MAXSOCKFD 10
#define SERVER_IP "127.0.0.1"
main()
{
int sockfd,newsockfd,is_connected[MAXSOCKFD],fd;
struct sockaddr_in addr;
int addr_len = sizeof(struct sockaddr_in);
fd_set readfds;
char buffer[256];
char msg[] = "welcome to server\n";

if((sockfd=socket(AF_INET, SOCK_STREAM, 0))<0)
{
perror("\n socket\n");
exit(1);
}

bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = htonl(INADDR_ANY);

if(bind(sockfd, &addr, sizeof(addr))<0)
{
perror("\n connect\n");
exit(1);
}

if(listen(sockfd, 3)<0)
{
perror("\n listen\n");
exit(1);
}

for(fd=0; fd<10; fd )
{
is_connected[fd]=0;
}

while(1)
{
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
for(fd=0; fd<10; fd )

if(is_connected[fd])
FD_SET(fd, &readfds);

if(!select(MAXSOCKFD, &readfds, NULL, NULL, NULL))
continue;

for(fd=0; fd<10; fd )

if(FD_ISSET(fd, &readfds))
{

if(sockfd==fd)
{
if((newsockfd=accept(sockfd, &addr, &addr_len))< 0)

perror("\n accept\n");

write(newsockfd, msg, sizeof(msg));
is_connected[newsockfd]=1;
printf("connect from %s\n",inet_ntoa(addr.sin_addr));

}
else
{
bzero(buffer, sizeof(buffer));

if(read(fd, buffer, sizeof(buffer))<=0)
{
printf("\n ==>connect closed\n");
is_connected[fd]=0;
close(fd);
}
else
{
printf("\n==>%s\n", buffer);
}
// Write Data Back to Client
if( write(fd, buffer, strlen(buffer) 1) == -1 )
{
printf( "Error: write()\n");
exit(1);
}
// Write String to Client
//printf("read=%d",read(STDIN_FILENO, buffer, sizeof(buffer)));
if(read(STDIN_FILENO, buffer, sizeof(buffer)))
bzero(buffer, sizeof(buffer));
read(STDIN_FILENO, buffer, sizeof(buffer));
// Write String to Server
if(send(sockfd, buffer, sizeof(buffer), 0)<0)
{
perror("\n send\n");
exit(1);
}

}
}
}
}

client.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 2345
#define SERVER_IP "127.0.0.1"
#define MAXSOCKFD 10
main()
{
int s;
struct sockaddr_in addr;
char buffer[256];
char obuf[256];

if((s=socket(AF_INET, SOCK_STREAM, 0))<0)
{
perror("\n socket\n");
exit(1);
}

bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = inet_addr(SERVER_IP);

if(connect(s, &addr, sizeof(addr))<0)
{
perror("\n connect\n");
exit(1);
}

recv(s, buffer, sizeof(buffer), 0);
printf("\n ==>%s\n",buffer);

while(1)
{
bzero(buffer, sizeof(buffer));
read(STDIN_FILENO, buffer, sizeof(buffer));
// Write String to Server
if(send(s, buffer, sizeof(buffer), 0)<0)
{
perror("\n send\n");
exit(1);
}
// Read String from Server
read(s, obuf, sizeof(obuf));
printf("[Echo From Server] %s\n", obuf);
printf("read=%d\n", read(s, obuf, sizeof(obuf)));

bzero(obuf, sizeof(obuf));

if(read(s, obuf, sizeof(obuf))<=0)
{
printf("\n ==>connect closed\n");
//is_connected[fd]=0;
//close(fd);
exit(1);
}
else
{
printf("\n==>%s\n", obuf);
}
}
}
編輯記錄
taishyang 重新編輯於 2009-04-22 11:14:22, 註解 移除[急急]字樣‧
系統時間:2024-11-21 16:33:16
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!