Linux下socket TCP的简单例子

这篇博客展示了在Linux环境下使用TCP套接字进行简单通信的示例,包括服务器端和客户端的C语言代码实现。通过创建socket、连接、发送和接收数据,实现了客户端向服务器发送消息并接收响应的功能。
摘要由CSDN通过智能技术生成

2011年2月22日

源代码:已经上传至我的资源

服务器端:

#include<sys/socket.h>
#include <unistd.h>// for close function
#include <string.h> // for bzero function
#include<stdio.h>
#include<sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<netinet/in.h>
#include <stdlib.h>
#define SERV_PORT 5555
#define SERV_IP "xxx.xxx.xxx.xxx"//mx27 board ip
#define BACKLOG 10 //the counts of connect can keep in wait queen
#define MAXBUFSIZE 200
char buf[MAXBUFSIZE]; //receive buf 
char str_to_send[200] ="important notice:to cerebrate China log on moon successful on 12nd,Jan,2010. everyone has a day free from work!/n"; 
int main(int argc, char **argv)
{
	int sockfd,sockfd_client;
	socklen_t sin_size; // used in accept(),but i don't know what it means
	printf("#####################################################/n");
	printf("socket receive text        by pafone  30th,April,2009/n");
	printf("server ip:%s port:%d         /n",SERV_IP,SERV_PORT);
	printf("#####################################################/n");
	struct sockaddr_in my_addr;//local ip info
	struct sockaddr_in serv_addr,client_sockaddr; //server ip info
	int serverport;
	if(argc == 2)
	{
		serverport = atoi(argv[1]);
	}
	else
	{
		serverport = SERV_PORT;
	}
	if(-1 == (sockfd = socket(AF_INET,SO
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值