Advertisement

01.20.2008 at 08:34PM PST, ID: 23097462 | Points: 500
[x]
Attachment Details

PHP send packet to server.

Asked by tesspool43 in PHP Scripting Language, Hypertext Transfer Protocol (HTTP), Web Development

In the code snippet I am using someone elses code which opens a socket to a server and then reads the incoming packets to get the data out of it. When its done it closes the socket. This part works fine, except that it incorrectly closes the connection to the server and the server makes a "user has a computer termination error". I am confident the way to combat that is that it has to send the correct packet to the server telling it goes to leave now.

I used wireshark to sniff the packets and the data for the packet to close out of the server is the following:
20657962000000002E000000

This ends up being something like  eyb. Which is backwards for bye. So its the correct packet data, but now I just need an official way to send it to the server and have the server accept it.

I tried doing something like that with the following code:


                        $packet = 0x20657962000000002E000000;
                           fwrite($fp,$packet);

But that does not seem to work, thus i commented it out. If anyone can give me more information on how I can send the BYE packet to the server and have it correctly disconnect. Rather I need more packet information from wireshark or not, let me know.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
<?php
	@set_time_limit(256);
 
	class PServer
	{
		var $svr;
		var $os;
		var $type;
		var $hits;
		var $usrs;
		var $max;
		var $http;
		var $ver;
 
		function GetServerInfo($host, $port)
		{
			if ($fp = @fsockopen($host, $port, $errno, $errstr, 5))
			{
				$prcl = @unpack("L*", @fread($fp, 1024));
				$this->hits = $prcl[3];
				$this->type = ($prcl[1] == 0x74697972 ? 0 : 1); // ryit = Intel, tiyr = Motorola
				$t = ($this->type ? "N" : "V");
				@fputs($fp, @pack("{$t}*", 0x73496E66, 0x10, $this->hits, 0x7E, 0x54595045, 0x04, 0x4A617661)); // sInf
				$prcl = $this->PopStack("{$t}3", @fread($fp, 1024), 12);
				if ($prcl[1] == 0x73496E66 && $prcl[3] == $this->hits) // sInf
				{
					while ($prcl[2])
					{
						$prcl = $this->PopStack("{$t}2", @end($prcl), 8);
						$prot = $prcl[1];
						$prcl = $this->PopStack("", @end($prcl), $prcl[2]);	
						$prcl[1] = @preg_replace("#\'#", "\\\'", $prcl[1]);
						switch($prot)
						{
							case 0x4E414D45: // NAME  // $prcl[1]
								$chars_to_htmlent = array(
								'¬' => '&#128;',
								'?' => '&#129;',
								'' => '&#130;',
								'’' => '&#131;',
								'' => '&#132;',
								'&' => '&#133;',
								' ' => '&#134;',
								'!' => '&#135;',
								'Æ' => '&#136;',
								'0' => '&#137;',
								'`' => '&#138;',
								'9' => '&#139;',
								'R' => '&#140;',
								'?' => '&#141;',
								'}' => '&#142;',
								'?' => '&#143;',
								'?' => '&#144;',
								'' => '&#145;',
								'' => '&#146;',
								'' => '&#147;',
								'' => '&#148;',
								'"' => '&#149;',
								'' => '&#150;',
								'' => '&#151;',
								'Ü' => '&#152;',
								'"' => '&#153;',
								'a' => '&#154;',
								':' => '&#155;',
								'S' => '&#156;',
								'?' => '&#157;',
								'~' => '&#158;',
								'x' => '&#159;');
								$this->svr = strtr(htmlentities($prcl[1]), $chars_to_htmlent);
 
								break;
							case 0x4855524C: // HURL
								$this->http = $prcl[1];
								preg_match("/http:\/\/(.*)\/media\//", $this->http, $matches);
								$this->user = $matches[1];
								break;
							case 0x4E555352: // NUSR
								$this->usrs = @end(@unpack("{$t}1", $prcl[1]));
								break;
						}
					}
				}
				print "$this->usrs <a href='palace://$this->user:$port'>$this->svr</a> <BR>\n";
 
				//$packet = 0x20657962000000002E000000;
         			//fwrite($fp,$packet);
				fclose($fp);
				
				return TRUE;
			} else {
				return FALSE;
			}
		}
 
		function PopStack($format = "", $data = "", $size = 0)
		{
			if ($format)
			{
				$ary = @unpack($format, $data);
				$ary[] = @substr($data, $size);
			} else {
				$ary = @array(1 => @substr($data, 0, $size), 2 => @substr($data, $size));
			}
			if (@count($ary))
			{
				return $ary;
			}
		}
	}
 
	$pserver = new PServer;
	$pserver->GetServerInfo("38.101.159.2", "5555");
	print "----------- AWAITING IP ---------<BR>\n";
?>
[+][-]01.21.2008 at 03:46AM PST, ID: 20705185

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 12:26PM PST, ID: 20709328

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628