Created
March 2, 2022 05:10
-
-
Save Park-Developer/fe8e33aaa138651cf7b7e8354fa4287a to your computer and use it in GitHub Desktop.
TCP Test : Python Client - C Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect(("WIFI ADDRESS", 5101)) | |
while(True): | |
# 접속할 서버의 ip주소와 포트번호를 입력. sock.send('Hello'.encode()) | |
inputdata=input() | |
sock.send(inputdata.encode()) # 내가 전송할 데이터를 보냄. | |
data=sock.recv(1024) | |
print(data.decode('utf-8')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#define TCP_PORT 5101 | |
int main(int argc, char **argv) | |
{ | |
int ssock; | |
socklen_t clen; // 소켓 디스크립터 정의 | |
int n; | |
struct sockaddr_in servaddr, cliaddr; // 구조체 정의 | |
char mesg[BUFSIZ]; | |
// 소켓 생성j | |
if((ssock=socket(AF_INET,SOCK_STREAM,0))<0){ | |
perror("socket()"); | |
return -1; | |
} | |
// 주소 구조체에 주소 지정 | |
memset(&servaddr,0,sizeof(servaddr)); | |
servaddr.sin_family=AF_INET; | |
servaddr.sin_addr.s_addr=inet_addr("WIFI ADDRESS"); | |
servaddr.sin_port=htons(TCP_PORT); // 사용할 포트 지정 | |
// bind 함수를 사용하여 서버 소켓의 주소 설정 | |
if(bind(ssock,(struct sockaddr *)&servaddr,sizeof(servaddr))<0){ | |
perror("bind()"); | |
return -1; | |
} | |
// 동시에 접속하는 클라이언트의 처리를 위한 대기 큐를 설정 | |
if(listen(ssock,8)<0){ // 최대 8개의 클라이언트가 동시 접속 대기할 수 있도록 설정 | |
perror("listen()"); | |
return -1; | |
} | |
clen=sizeof(cliaddr); | |
char addr[100]; // TCP Client Address | |
char sene_msg[100]="test message"; // send message | |
int rn, csock = accept(ssock,(struct sockaddr *)&cliaddr,&clen); | |
// 네트워크 주소를 문자열로 변경 | |
inet_ntop(AF_INET,&cliaddr.sin_addr,addr,sizeof(addr)); | |
printf("Client is conneted : %s\n",addr); | |
do{ | |
// 클라이언트가 접속하면 접속을 허용하고 클라이언트 소켓 생성 | |
/* | |
int n, csock = accept(ssock,(struct sockaddr *)&cliaddr,&clen); | |
// 네트워크 주소를 문자열로 변경 | |
inet_ntop(AF_INET,&cliaddr.sin_addr,addr,sizeof(addr)); | |
printf("Client is conneted : %s\n",addr); | |
*/ | |
if ((rn=read(csock, mesg,sizeof(mesg)))<=0) | |
perror("read()"); | |
//bzero(mesg,sizeof(mesg)); | |
printf("Received Data : %s\n",mesg); | |
bzero(mesg,sizeof(mesg)); | |
// 클라이언트로 buf에 있는 문자열 전송 | |
if(write(csock,sene_msg,sizeof(sene_msg))<=0) | |
perror("write()"); | |
//close(csock); | |
}while(strncmp(mesg,"q",1)); | |
close(ssock); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment