`
myten
  • 浏览: 134172 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Socket Server By C

 
阅读更多
/*
 ============================================================================
 Name        : TestServer.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/in.h>
#include <string.h>

int main(void) {
	int sfp, nfp;
	int sin_size;
	struct sockaddr_in s_add, c_add;
	int port = 8888;
	sfp = socket(AF_INET, SOCK_STREAM, 0);
	if (sfp == -1) {
		printf("socket build fail\r\n");
		return -1;
	}
	printf("socket ok!\r\n");

	bzero(&s_add, sizeof(struct sockaddr_in));
	s_add.sin_family = AF_INET;
	s_add.sin_addr.s_addr = htonl(INADDR_ANY);
	s_add.sin_port = htons(port);
	if (-1
			== bind(sfp, (struct sockaddr *) (&s_add),
					sizeof(struct sockaddr))) {
		printf("bind error\r\n");
		return -1;
	}
	printf("bind ok\r\n");
	if (-1 == listen(sfp, 5)) {
		printf("listen fail ! \r\n");
		return -1;
	}
	while (1) {
		sin_size = sizeof(struct sockaddr_in);
		nfp = accept(sfp, (struct sockaddr *) (&c_add), &sin_size);
		if (-1 == nfp) {
			printf("accept error \r\n");
			return -1;
		}
		printf("accept ok \r\n");
		write(nfp,"hello,welcome to my server \r\n", 32);

	}
	return EXIT_SUCCESS;
}

 

分享到:
评论

相关推荐

    socket简易网络编程

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(('localhost', 12345)) server_socket.listen(5) while True: client_socket, addr = server_socket.accept() print(f'...

    socket简单用例

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(('localhost', 12345)) server_socket.listen(1) ``` 2. **接受客户端连接(Accept Connection)**: - 当有客户端...

    实战Linux Socket 编程-示例源码

    通过阅读和实践《Linux_Socket_Programming_By_Example》中的源码,你可以更深入地理解这些概念,并掌握Linux Socket编程的实际操作。记得结合`man`手册页学习相关的系统调用细节,这将有助于你在实际开发中灵活应用...

    linux socket programming

    《Linux Socket Programming by Example》一书不仅详细解释了这些概念,还提供了丰富的实例,帮助读者深入理解并实践Linux Socket编程。无论是初学者还是有一定经验的开发者,都可以从这本书中获得宝贵的知识和技能...

    基于Wifi C/S模式 Socket通讯

    **基于Wifi C/S模式 Socket通讯详解** 在计算机网络通信中,C/S(Client/Server)模式是一种常见的架构设计,它由客户端(Client)和服务器端(Server)组成。在这种模式下,客户端发起请求,服务器端负责处理请求...

    Linux系统socket通信程序

    在压缩包文件"Linux_Socket_Programming_By_Example"中,可能包含了一系列示例代码,如服务器端(server.c)和客户端(client.c)的实现,演示了如何创建、连接、交换数据并关闭Socket。这些例子可以帮助开发者理解...

    套接字(socket)

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(('localhost', 12345)) server_socket.listen(1) while True: client_socket, addr = server_socket.accept() print(f'...

    hands-network-programming-c-optimized.rar

    By the end of this book, you’ll have experience of working with client-server applications, and be able to implement new network programs in C. The code in this book is compatible with the older C99 ...

    python 和C sokcket 通信

    Python和C语言之间的Socket通信是跨语言网络编程的一个常见应用场景。在互联网协议栈中,Socket是一种接口,允许程序通过网络发送和接收数据。本篇将深入探讨如何使用Python的socket库与C语言的socket API进行通信。...

    IRC-chatSystem:linux socket网络编程 by c program,IRC-chatSystem

    IRC-chatSystem IRC简单聊天系统 IRC simple chat system linux大型实验 ...源代码一份 client.c server.c (含user.ini) 技术说明文档一份 IRC软件技术报告.pdf 使用说明文档一份 IRC软件使用说明书.pd

    计算机网络第六版答案

    This document contains the solutions to review questions and problems for the 5th edition of Computer Networking: A Top-Down Approach by Jim Kurose and Keith Ross. These solutions are being made ...

    vue-socket.io跨域问题有效解决方法

    Access to XMLHttpRequest at ‘http://192.168.37.130:5050/socket.io/?EIO=3&transport=polling&t=N0oqNsW’ from origin ‘http://localhost:8080’ has been blocked by CORS policy: No ‘Access-Control-Allow...

    Binl_Server.zip

    * Mini Binl Server * Copyright (c) 2005-2007 Gianluigi Tiesi * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * ...

    Serial Port Server with sample code

    More jobs can be done by implementing the client ;; utilities. It is listening on `[Listen_Port]/Data' port. ;; ;;------------------------------------------------------------------------------ ;; Tip...

    C语言浏览器和http服务器实验报告(含代码)

    - 服务器端代码通常包含主程序(如server.c)和辅助函数(如request_handler.c)。 - 浏览器端代码可能包含main.c,用于处理用户输入和发送请求。 2. **其他文件**: - 需要静态资源文件,如HTML页面,供服务器...

    Modbus TCP客户端和服务器示例

    **Modbus TCP客户端和服务器示例** 在工业自动化和物联网(IoT)领域,Modbus是一种广泛使用的通信协议,它允许设备之间进行简单的数据交换。本文将深入探讨Modbus TCP,这是一种在网络环境中运行的Modbus变体,尤其...

    CentOS 7 安装Percona Server+Mysql

    Description=Percona Server (MySQL) instance managed by MySQL Server After=network.target [Service] User=mysql ExecStart=/usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/dataconf Restart=...

    Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: ….. this is incompatible with sq

    3、Can’t connect to local MySQL server through socket ‘/Data/mydata/mysql.sock’ socket文件目录不对应导致的问题 4、今天要说的就是 没有打开only_full_group_by Cause:...

    UNIX Network Programming Volume 1, Third Edition (Unix网络编程卷1第3版英文版)

    Protocol Usage by Common Internet Applications Section 2.14. Summary Exercises Part 2: Elementary Sockets Chapter 3. Sockets Introduction Section 3.1. Introduction Section 3.2. Socket...

Global site tag (gtag.js) - Google Analytics