`
myten
  • 浏览: 131965 次
  • 性别: 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;
}

 

分享到:
评论

相关推荐

    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 ...

    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 ...

    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...

    TCP/IP Sockets in C 2rd Edition

    An additional chapter on socket programming in C++ (contributed by David B. Sturgill). The PracticalSocket library provides wrappers for basic socket functionality. These allow an instructor to ...

    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...

    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:...

    ssl_data_transfer

    socket连接,使用ssl建立安全通道...Makefile: used to generate app_client and app_server by client.c and server.c client.c:the client use ssl to transfer data server.c: the server use ssl to transfer data

    Modbus TCP客户端和服务器示例

    基于我们开源的Modbus协议栈实现的Modbus TCP客户端和服务器示例,使用VS2019开发。

    linux 下 带shell功能ftp源代码

    this server and client programs is created for the s3c2410 platform board to conveniencely control it and fulfil files transferring at the same time , that is a simple combination for telnet and ...

    linux 下 结合ftp 和部分telnet 功能的服务器源码

    this server and client programs is created for the s3c2410 platform board to conveniencely control it and fulfil files transferring at the same time , that is a simple combination for telnet and ...

    PHP Reactive Programming

    The book will then focus on writing extendable RxPHP code by developing a code testing tool and also cover Using RxPHP on both the server and client side of the application. With a concluding chapter...

    Bochs - The cross platform IA-32 (x86) emulator

    Changes in 2.4.6 (February 22, 2011): Brief summary : - Support more host OS to run on: - Include win64 native binary in the ... [2914433] makesym.perl misses symbols by John R. Jackson [2908481] USB ...

    检查网站是否存在 opensSSL

    # Quick and dirty demonstration of CVE-2014-0160 by Jared Stafford (jspenguin@jspenguin.org) # The author disclaims copyright to this source code. import sys import struct import socket import time ...

    ICS delphixe10源码版

    .\Samples\delphi\SocketDemos Delphi Win32/Win64 Socket sample applications (all Delphi versions) .\Samples\delphi\sslinternet Delphi Win32/Win64 SSL-enabled sample applications (all Delphi versions) ....

    SocketIO包括客户端和服务器端代码

    Usage example: 1. Build or install ...2. Switch to /server folder and build server by maven. 3. Run server by command `mvn exec:java` 4. Run client in browser, by opening* file /client/index.html

    可以仿造ip

    /* 本程序用Visual C++ 6.0编译在Windows 2000 Advanced Server 上调试通过 */ /* Created by janker@371.net 2000.8.28 */ /* 声明:本程序经修改后可能会产生攻击性擅自修改成攻击程序者后果...

Global site tag (gtag.js) - Google Analytics