mdebug

cortex m series debugger
git clone http://frotz.net/git/mdebug.git
Log | Files | Refs | README | LICENSE

socket.c (1258B)


      1 /* socket.c
      2  *
      3  * Copyright 2015 Brian Swetland <swetland@frotz.net>
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 #include <unistd.h>
     19 #include <string.h>
     20 #include <sys/types.h> 
     21 #include <sys/socket.h>
     22 #include <netinet/in.h>
     23 
     24 int socket_listen_tcp(unsigned port) {
     25 	int fd, n = 1;
     26 	struct sockaddr_in addr;
     27 
     28 	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
     29 		return -1;
     30 	}
     31 
     32 	setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n));
     33 
     34 	memset(&addr, 0, sizeof(addr));
     35 	addr.sin_family = AF_INET;
     36 	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
     37 	addr.sin_port = htons(port);
     38 	if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
     39 		goto fail;
     40 	}
     41 	if (listen(fd, 10) < 0) {
     42 		goto fail;
     43 	}
     44 	return fd;
     45 fail:
     46 	close(fd);
     47 	return -1;
     48 }