이게 서버 소스..
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <signal.h>
#define Maxlen 1024
#define ERR_EXIT(m) \
do { \
perror(m); \
exit(EXIT_FAILURE); \
} while (0)
void do_service(int);
int main(void)
{
long maxlen;
signal(SIGCHLD, SIG_IGN);//좀비 프로세스 이용
int listenfd; //used for listening
if ((listenfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
// listenfd = socket(AF_INET, SOCK_STREAM, 0)
ERR_EXIT("socket error");
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;//Internet Address cluster
servaddr.sin_port = htons(4567);//Port Number
//servaddr.sin_addr.s_addr = htonl(INADDR_ANY);//IP address
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* inet_aton("127.0.0.1", &servaddr.sin_addr); */
int on = 1;
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
ERR_EXIT("setsockopt error");
if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
ERR_EXIT("bind error");
if (listen(listenfd, SOMAXCONN) < 0)
ERR_EXIT("listen error");
struct sockaddr_in peeraddr; //construct the struct for sockaddr_in
socklen_t peerlen = sizeof(peeraddr); //needs to be initialized
int conn; // connected sockfd
pid_t pid;
while (1)
{
if ((conn = accept(listenfd, (struct sockaddr *)&peeraddr, &peerlen)) < 0)
ERR_EXIT("accept error");
printf("recv connect ip=%s port=%d\n", inet_ntoa(peeraddr.sin_addr),
ntohs(peeraddr.sin_port));
pid = fork();
if (pid == -1)
ERR_EXIT("fork error");
if (pid == 0)
{
// 자식 프로세스
close(listenfd);
do_service(conn);
exit(EXIT_SUCCESS);
}
else
close(conn); //부모 프로세스
}
return 0;
}
void do_service(int conn)
{
char recvbuf[1024];
long maxlen;
while (1)
{
memset(recvbuf, 0, sizeof(recvbuf));
int ret = read(conn, recvbuf, sizeof(recvbuf));
if (ret == 0) //클라이언트 종료
{
printf("client close\n");
break;
}
else if (ret == -1)
ERR_EXIT("read error");
fputs(recvbuf, stdout);
//printf("msg received from client:%s\n",recvbuf );
write(conn, "msg received\n", 1024);
}
}
=======================
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#define ERR_EXIT(m) \
do { \
perror(m); \
exit(EXIT_FAILURE); \
} while (0)
int main(int argc,char* argv[])
{
int sock,rec_len;
struct sockaddr_in servaddr;
char sendbuf[1024] = {0};
char recvbuf[1024] = {0};
char ipaddress[20];
/*if(argc!=2)
{
printf("parameter wrong:which should be like this --ppclient 127.0.0.1 4567\n");
exit(0);
}*/
if(argc!=4)
{
printf("parameter wrong:which should be like this --ppclient 127.0.0.1 4567\n");
exit(0);
}
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
// listenfd = socket(AF_INET, SOCK_STREAM, 0)
ERR_EXIT("socket error");
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
//servaddr.sin_port = htons(4567);
servaddr.sin_port=htons(atoi(argv[3]));
//servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* inet_aton("127.0.0.1", &servaddr.sin_addr); */
memset(ipaddress,0,sizeof(ipaddress));
//sprintf(ipaddress,"%s",argv[1]);
sprintf(ipaddress,"%s",argv[2]);
if(inet_aton(ipaddress,&servaddr.sin_addr)==0)
{
printf("inet_aton error for :%s\n",ipaddress);
exit(0);
}
if (connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
ERR_EXIT("connect error");
printf("send msg to server:\n");
while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)
{
write(sock, sendbuf, strlen(sendbuf));
read(sock, recvbuf, sizeof(recvbuf));
//printf("Received msg:%s\n",recvbuf);
fputs(recvbuf, stdout);
memset(sendbuf, 0, sizeof(sendbuf));
memset(recvbuf, 0, sizeof(recvbuf));
}
close(sock);
return 0;
}
이게 클라이언트 소스인데요
클라이언트를 리눅스에서 컴파일해서 실행하면
parameter wrong:which should be like this --ppclient 127.0.0.1 4567
이렇게 뜨는데 포트가 열려있지않다는 말인가요??
해결방법좀 알려주세요 ㅠㅠ