Last active
May 5, 2017 13:02
-
-
Save katsuobushiFPGA/70712617cbcf36a1c49385063eebe500 to your computer and use it in GitHub Desktop.
Socket
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 <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <memory.h> | |
int | |
main() | |
{ | |
struct sockaddr_in server; | |
int sock; | |
char buf[1024]; | |
int n; | |
int isCon; | |
/* ソケットの作成 */ | |
sock = socket(AF_INET, SOCK_STREAM, 0); | |
/* 接続先指定用構造体の準備 */ | |
server.sin_family = AF_INET; | |
server.sin_port = htons(12345); | |
server.sin_addr.s_addr = inet_addr("192.168.3.9"); | |
/* サーバに接続 */ | |
isCon = connect(sock, (struct sockaddr *)&server, sizeof(server)); | |
if(isCon < 0) { | |
printf ("can't connect server\n"); | |
return -1; | |
} | |
/* サーバからデータを受信 */ | |
memset(buf, 0, sizeof(buf)); | |
while(1) { | |
memset(buf,0,sizeof(buf)); | |
scanf("%s",buf); | |
//send packet | |
write(sock,buf,sizeof(buf)); | |
n = read(sock, buf, sizeof(buf)); | |
printf("%s\n", buf); | |
} | |
/* socketの終了 */ | |
close(sock); | |
return 0; | |
} |
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
#!/bin/sh | |
# chkconfig: 2345 91 91 | |
# description: test script. | |
. /etc/rc.d/init.d/functions | |
PATH=/sbin:/bin:/usr/sbin:/usr/bin | |
PROG=/usr/local/sbin/serverd | |
PROGNAME=`basename $PROG` | |
[ -f $PROG ] || exit 0 | |
case "$1" in | |
start) | |
echo -n $"Starting $PROGNAME: " | |
daemon $PROG | |
echo | |
;; | |
stop) | |
echo -n $"Stopping $PROGNAME:" | |
killproc $PROGNAME | |
echo | |
;; | |
status) | |
status $PROGNAME | |
;; | |
*) | |
echo $"Usage: $PROGNAME {start|stop|status}" >&2 | |
exit 1 | |
;; | |
esac | |
exit 0 |
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 <stdlib.h> | |
#include <memory.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
int analyze_packet(char *buf); | |
int | |
main() | |
{ | |
int sock0; | |
struct sockaddr_in addr; | |
struct sockaddr_in client; | |
int len; | |
int sock; | |
char buf[1024]; | |
int n; | |
if(daemon(0,0) != 0) { | |
fprintf(stderr,"deamonize failed\n"); | |
exit(1); | |
} | |
/* ソケットの作成 */ | |
sock0 = socket(AF_INET, SOCK_STREAM, 0); | |
/* ソケットの設定 */ | |
addr.sin_family = AF_INET; | |
addr.sin_port = htons(12345); | |
addr.sin_addr.s_addr = INADDR_ANY; | |
bind(sock0, (struct sockaddr *)&addr, sizeof(addr)); | |
/* TCPクライアントからの接続要求を待てる状態にする */ | |
listen(sock0, 5); | |
/* TCPクライアントからの接続要求を受け付ける */ | |
len = sizeof(client); | |
sock = accept(sock0, (struct sockaddr *)&client, &len); | |
while(1) { | |
//receive packet | |
n = read(sock,buf,sizeof(buf)); | |
if(n==0 || n== -1) { | |
close(sock); | |
break; | |
} | |
printf("%s",buf); | |
analyze_packet(buf); | |
//send packet | |
write(sock, buf, sizeof(buf)); | |
} | |
/* TCPセッションの終了 */ | |
close(sock); | |
/* listen するsocketの終了 */ | |
close(sock0); | |
return 0; | |
} | |
int analyze_packet(char *buf) { | |
int res = 0; //success | |
//00000001 case | |
if(strcmp("00000001",buf) == 0) { | |
memset(buf,0,sizeof(buf)); | |
memcpy(buf,"receive:OK",10); | |
} | |
else if(strcmp("00000002",buf) == 0) { | |
memset(buf,0,sizeof(buf)); | |
memcpy(buf,"test_schedule",13); | |
} | |
return res; | |
} |
catch error exception
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SELinux = Disabled
vi /etc/sysconfig/iptables
server side open port
-A INPUT -p tcp -m tcp --dport 12345 -j ACCEPT
-A OUTPUT -p tcp -m tcp --dport 12345 -j ACCEPT