tcp udp 的echo脚本

============================================================
博文原创,转载请声明出处
============================================================
在进行tcp和udp的通信测试时候,需要对应的测试脚本作支持,搜集了一些,和大家分享。


echo_tcp_server.h, tcp的echo程序,建立tcp连接后,自己发送什么消息,此程序回复同样消息。gcc编译后即可使用

echo_udp_server.pyudp 服务器端的echo程序,自己发送什么消息,此程序回复同样消息。

echo_udp_client.py udp客户端echo程序。

附程序如下——————————————————

echo_tcp_server.c

echo_tcp_server.c#include<stdio.h>
#include<unistd.h>
#include<sys/socket.h>
#include<arpa/inet.h>

#define		LISTEN_PORT		5000
#define		true			1
#define		false			0

int listen_sock, comm_sock;
struct sockaddr_in serv_addr, clnt_addr;

int server_init();
int is_accept();
int handle_echo();

int main(void){
	server_init();
	while(1){
		if(is_accept()){
			handle_echo();
		}
	}
	return 0;
}

int is_accept(){
	int clnt_addr_len = sizeof(clnt_addr);
	if((comm_sock = accept(listen_sock, (struct sockaddr *)&clnt_addr, &clnt_addr_len)) < 0){
		return false;
	}else{
		printf("Client(IP:%s) connected.\n", inet_ntoa(clnt_addr.sin_addr));
		return true;
	}
}

#define		BUFFER_SIZE		32
int handle_echo(){
	char buffer[BUFFER_SIZE];
	int recv_len;
	int goon = true;

	while(goon){
		if((recv_len = recv(comm_sock, buffer, BUFFER_SIZE, 0)) < 0){
			perror("recv() error.");
			exit(-1);
		}else if(recv_len == 0){
			goon = false;
		}else{
			if(send(comm_sock, buffer, recv_len, 0) != recv_len){
				perror("send() error.");
				exit(-1);
			}
		}
	}
}

int server_init(){
	// create listen socket
	if((listen_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
		perror("listen_socket error.");
		exit(-1);
	}

	// init the serv_addr
	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	serv_addr.sin_port = htons(LISTEN_PORT);

	// bind listen socket with server address
	if(bind(listen_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
		perror("bind() error.");
		exit(-1);
	}

	// listen on the port 
	if(listen(listen_sock, 10) < 0){
		perror("listen() error.");
		exit(-1);
	}
}
echo_udp_server.py

#!/usr/bin/env python
 
"""
A simple echo server (UDP)
"""
 
import socket
 
# define servr properties
host = ''
port = 9003
size = 8192
 
# configure server socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((host, port))
 
# wait for connections
# terminate with 
try:
  while True:
    data, address = sock.recvfrom(size)
    print "datagram from", address,data
    #print "data is ",data
    sock.sendto(data, address)
finally:
  sock.close()
 

echo_udp_client.py

#!/usr/bin/env python
 
"""
A simple echo client (UDP)
"""
 
import socket
 
# configure the client
port = 9003
host = 'localhost'
size = 8192
timeout = 8
testMsg = "This is my //77text123"
 
# initialize socket
try:
  sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  sock.settimeout(timeout)
  sock.sendto(testMsg, (host, port))
  response = sock.recv(8192)
  sock.close()
  print response
  if response == testMsg:
    print "connection is working fine."
  else:
    print "connection error occured."
 
except:
  print "cannot reach your server"



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值