概述
============================================================
博文原创,转载请声明出处
蓝岩--移动互联网老兵
============================================================
在进行tcp和udp的通信测试时候,需要对应的测试脚本作支持,搜集了一些,和大家分享。
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"
最后
以上就是单身黑夜为你收集整理的tcp udp 的echo脚本的全部内容,希望文章能够帮你解决tcp udp 的echo脚本所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复