summaryrefslogtreecommitdiffstats
path: root/code/jasmine/tcp-common.c
diff options
context:
space:
mode:
Diffstat (limited to 'code/jasmine/tcp-common.c')
-rwxr-xr-xcode/jasmine/tcp-common.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/code/jasmine/tcp-common.c b/code/jasmine/tcp-common.c
new file mode 100755
index 00000000..69a57ba0
--- /dev/null
+++ b/code/jasmine/tcp-common.c
@@ -0,0 +1,60 @@
+/*##############################################################################
+# Copyright (c) 2017 ZTE Coreporation and others.
+# hu.zhijiang@zte.com.cn
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################*/
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+
+#include "buffer.h"
+#include "tcp-common.h"
+#include "misc.h"
+
+int init_tcp_server_socket(char *addr, unsigned short port)
+{
+ int s;
+ struct sockaddr_in sin;
+
+ port = port ? port : TCP_DPORT;
+ sin = make_addr(addr, port);
+
+ if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+ crit("socket() failed");
+ }
+
+ if (bind(s, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
+ crit("bind() failed, error binding tcp socket");
+ }
+
+ if (listen(s, 5) < 0) {
+ crit("listen() failed, error listening on socket");
+ }
+
+ log(4, "init_tcp_server_socket %s:%d", inet_ntoa(sin.sin_addr), port);
+ return s;
+}
+
+int init_tcp_client_socket(char *addr, unsigned short port)
+{
+ int s;
+ struct sockaddr_in sin;
+
+ port = port ? port : TCP_DPORT;
+ sin = make_addr(addr, port);
+
+ if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+ crit("socket() failed");
+ }
+
+ if (connect(s, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
+ crit("connect() failed");
+ }
+
+ log(4, "init_tcp_client_socket %s:%d", inet_ntoa(sin.sin_addr), port);
+ return s;
+}