리눅스 소켓프로그래밍
새밝
질문 제목 : 리눅스 소켓프로그래밍한 컴퓨터로 할 땐 통신이 잘 되는데 두 대로 놓고 하면 안되는데.. 왜 안되는 지 이유를 모르겠어요 ㅠㅠ질문 내용 : 리눅스 c프로그래밍
서버
#include sys/types.h
#include sys/stat.h
#include sys/socket.h //소켓 관련 함수
#include unistd.h //각종 시스템 함수
#include netinet/in.h
#include arpa/inet.h //소켓 지원을 위한 각종 함수
#include stdio.h //표준 입출력 관련
#include stdlib.h
#include string.h //문자열 관련
int main(int argc, char **argv) //int argc, char **argv 는 main()함수의 매개 변수
{
int server_sockfd, client_sockfd;
int client_len;
struct sockaddr_in clientaddr, serveraddr; //ip와 port를 저장하는 구조체(서버, 클라이언트)
char buf[255]; //파일 출력할 때 사용
if (argc != 2) //argc가 두 개의 인자를 갖지 않으면
{
printf(usage : ./zipcode [port]\n);
printf(예 : ./zipcode 4444\n);
exit(0);
}
// internet 기반의 소켓 생성 (inet)
server_sockfd = socket(af_inet, sock_stream, 0);
// struct sockaddr_in 값 설정
bzero(&serveraddr, sizeof(serveraddr)); //무조건 0x00으로 초기화
serveraddr.sin_family = af_inet;
serveraddr.sin_addr.s_addr = htonl(inaddr_any);
serveraddr.sin_port = htons(atoi(argv[1])); //atoi(argv[1])에 포트번호가 들어간다. //바인드 설정 bind(듣기 소켓, ip와 port저장하는 구조체, 두 번째 인자의 데이터 크기)
bind(server_sockfd , (struct sockaddr *)&serveraddr, sizeof(serveraddr));
//리슨 설정 listen(socket함수 수행한 결과 얻은 듣기 소켓 지정 번호, 수신 대기열의 크기)
listen(server_sockfd, 5); //어셉트 설정 accept(클라이언트의 연결 요청을 받아들이는 듣기 소켓, 연결된 클라이언트의 주소와 포트번호 구조체에 저장, 구조체의 크기)
client_sockfd = accept(server_sockfd, (struct sockaddr *)&clientaddr, &client_len);
file *file = fopen(text, wt);
if(file == null) {
printf(file open error!);
return 1;
}
int i;
for(i=0; i1; i++) {
read (client_sockfd, buf, 255);
fputs(buf, file);
}
close(client_sockfd);
}
클라이언트
#include sys/types.h
#include sys/stat.h
#include sys/socket.h
#include unistd.h
#include netinet/in.h
#include arpa/inet.h
#include stdio.h
#include stdlib.h
#include string.h
int main(int argc, char **argv)
{
int client_len;
int client_sockfd;
char buf[255];
struct sockaddr_in clientaddr;
if (argc != 2)
{
printf(usage : ./zipcode_cl [port]\n);
printf(예 : ./zipcode_cl 4444\n);
exit(0);
}
//소켓 생성
client_sockfd = socket(af_inet, sock_stream, 0);
clientaddr.sin_family = af_inet;
clientaddr.sin_addr.s_addr = inet_addr(10.26.0.50);
clientaddr.sin_port = htons(atoi(argv[1]));
client_len = sizeof(clientaddr);
//connect 설정
connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len);
memset(buf, 0x00, 255);
file *file = fopen(text, rt);
if(file == null) {
printf(file open error!);
return 1;
& }
while( fgets(buf, 255, file) != null ) {
write(client_sockfd, buf, 255);
}
close(client_sockfd);
}
-
고딩
두 대를 허브를 거치지 말고 직접 연결해보세요.
-
마루
외부접속 허용 했나요 아니면 동적 아이피 접속 가능한가요?
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692374 | 고수님들 댓글 마니부탁해요!!! (2) | 엄지 | 2025-04-22 |
2692343 | scnaf에 자꾸 선언을 참조하라는데;; (8) | 도래 | 2025-04-22 |
2692282 | 도스상에서 생성된 exe파일에 press~ 뜨게 하기 (4) | 회사원 | 2025-04-21 |
2692256 | scanf("%*c"); ㅠㅠ 고수님들 | 거북이 | 2025-04-21 |
2692230 | 하노이탑 질문입니다. (1) | 미쁘다 | 2025-04-21 |
2692210 | 정보 올림피아드 문제인데.. 풀이 과정이 궁금합니다.(재귀함수) (5) | 물티슈 | 2025-04-20 |
2692144 | C언어와 리눅스에 대한 질문입니다. | 싴흐한세여니 | 2025-04-20 |
2692114 | 컨텍스트 스위칭하는데 걸리는 시간 측정.. | YourWay | 2025-04-19 |
2692086 | 간접참조 연산자, 증감연산자 질문이용! (2) | 블랙캣 | 2025-04-19 |
2692056 | 주석좀 달아주세요. 몇개적엇는데 몇개만달아주세요. (2) | DevilsTears | 2025-04-19 |
2691978 | 진수 쉽게 이해하는법... (3) | 지지않는 | 2025-04-18 |
2691949 | getchar() 한 문자를 입력받는 함수 질문 | 채꽃 | 2025-04-18 |
2691919 | 배열 정렬 및 합치기 질문입니다. | 사과 | 2025-04-18 |
2691845 | c언어왕초보 질문이 있습니다........ | 루나 | 2025-04-17 |
2691815 | void add(int num); 함수... (4) | 살랑살랑 | 2025-04-17 |
2691756 | 명령 프롬프트 스크롤바가 없어요 | 두메꽃 | 2025-04-16 |
2691725 | 자료구조에 관련해서 질문이 있어 글을 올립니다. | 누리알찬 | 2025-04-16 |
2691697 | if 문에서 구조체 배열에 저장되있던 문자열 검사하는 법 ? (2) | 민트맛사탕 | 2025-04-16 |
2691678 | C언어 함수 질문이요~!!! | 연보라 | 2025-04-15 |
2691650 | 반복문 | 돋가이 | 2025-04-15 |