socket 发信息 集合(整理中)以后可能大家发出来各种使用socket发消息的代码,干脆做个整理吧,方便大家使用 1 ruby版本 #!/usr/bin/ruby require 'socket' fetion = UDPSocket::new fetion.connect("192.168.1.222",40000) fetion.send("sms 0 hi,ruby",0) fetion.close 2 perl版本 http://www.it-adv.net/bbs/viewthread.php?tid=22&extra=page%3D1&frombbs=1 #!/usr/bin/perl -w use Socket; $proto = getprotobyname('udp'); socket(SMS,AF_INET,SOCK_DGRAM,$proto) || die "socket: $!"; $port = 40000; $inaddr = inet_aton('192.168.1.222'); $paddr = sockaddr_in($port,$inaddr); connect(SMS,$paddr) || die "connect: $!"; print SMS "sms 0 perl"; close SMS; perl版本 0.2 简要说明一下,大师级的别丢鸡蛋我 下面这个就是多加了一个编码转换的功能,我用的linux一般是用的gb2312的编码,但是机器人是使用的utf8的编码,所以在发送给飞信之前先转换一下 大家如果和我情况一样可以修改为你们自己的编码类型,然后转为utf8 #!/usr/bin/perl -w use Encode qw/from_to/; use Socket; $msg = $ARGV[0]; $port = 40000; $ip = '192.168.1.222'; $proto = getprotobyname('udp'); socket(SMS,AF_INET,SOCK_DGRAM,$proto) || die "socket: $!"; $inaddr = inet_aton($ip); $paddr = sockaddr_in($port,$inaddr); connect(SMS,$paddr) || die "connect: $!"; $smsg="sms 0 $msg"; from_to($smsg,"gb2312","utf8"); print SMS $smsg; close SMS; 3 python版 http://www.it-adv.net/bbs/viewthread.php?tid=17&extra=page%3D1 import socket HOST='localhost' PORT=40000 s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) try: s.connect((HOST,PORT)) except socket.error: print 'failed to connect' s.close() s.send('chat 0 生命短暂') s.close() 4 c版 http://www.it-adv.net/bbs/viewthread.php?tid=16&extra=page%3D1 C语言SOCKET编程,使用机器人发送短信 #include <math.h> #include <string.h> #include <sys pes.h> #include <netinet> #include <sys> #include <errno.h> #include <stdlib.h> #include <arpa> #include <sys> #define MAXBUF 1024 int main(int argc, char **argv) { char buff[MAXBUF]; bzero(buff,sizeof(buff)); int Sockfd; struct sockaddr_in RobotAddr; if ((Sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { printf("Socket error\n"); return -1; } printf("create socket succeed\n"); memset(&RobotAddr, 0, sizeof(struct sockaddr_in)); /* 设置地址和端口信息 */ RobotAddr.sin_family = AF_INET; RobotAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); RobotAddr.sin_port = htons(40000); sprintf(buff,"sms 0 This is just a test programme.",); int SendBytes = sendto(Sockfd, buff, strlen(buff), 0, (struct sockaddr *)&RobotAddr,sizeof(RobotAddr)); if(SendBytes 6 c#版 http://www.it-adv.net/fetion/fetion_dotnet.rar 7 java版 http://www.it-adv.net/it-adv.net/bbs/viewthread.php?tid=10&extra=page%3D1 JAVA与飞信机器人 java如何调用飞信机器人进行发送短信呢?请参考以下实例: ------------------------------------------------------------------------------------------------- import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class TestUDP { /** * @param args */ public static void main(String[] args) throws Exception{ DatagramSocket datagramsocket = new DatagramSocket(); // str="sms 0 hello"; String str="sms 13406956704 回短信呀"; // 格式:sms 手机号 短信内容. 手机号为0时代表自己 InetAddress ia=InetAddress.getByName(args[0]); DatagramPacket p=new DatagramPacket(str.getBytes(), str.getBytes().length, ia, Integer.parseInt(args[1])); datagramsocket.send(p); System.out.println("has send to ["+args[0]+":"+args[1]+"]"); } } ------------------------------------------------------------------------------------------------------- >java TestUDP 127.0.0.1 40000 8 待整理</sys></arpa></stdlib.h></errno.h></sys></netinet></sys></string.h></math.h>
如何使用socket发送信息(飞信机器人)
最新推荐文章于 2022-05-09 14:21:31 发布