Ϊʲôǰ̨ºÍºǫ́²»ÄÜͨÐÅ£¿
ÕâÊÇ¿Í»§¶Ë£¨ÓÃphp£©<?php
$server_ip="127.0.0.1";
$port = 123434;
if($_POST['submit']){
$buf=trim($_POST['buf']);
$sockfd=socket_create(AF_INET,SOCK_STREAM,SOL_TCP)or die("´´½¨Ê§°Ü");
socket_connect($sockfd,$server_ip,$port)or die("Á¬½Ó´íÎó");
//$buf=$_POST['buf'];
socket_write($sockfd,$buf,strlen($buf))or die("дÈë´íÎó");
$a=socket_read($sockfd,4096);
echo "the message from server: ".$a."<br />";
$buf="";
socket_close($sockfd);
//unset($_POST['submit']);
}
?>
<form action="tcpclient.php" method="post">
ÇëÑ¡Ôò·¢ËÍ·½Ê½£º<br><input type=text name="buf">
<input type=submit name="submit" value="·¢ËÍ">
</form>
$server_ip="127.0.0.1";
$port = 123434;
if($_POST['submit']){
$buf=trim($_POST['buf']);
$sockfd=socket_create(AF_INET,SOCK_STREAM,SOL_TCP)or die("´´½¨Ê§°Ü");
socket_connect($sockfd,$server_ip,$port)or die("Á¬½Ó´íÎó");
//$buf=$_POST['buf'];
socket_write($sockfd,$buf,strlen($buf))or die("дÈë´íÎó");
$a=socket_read($sockfd,4096);
echo "the message from server: ".$a."<br />";
$buf="";
socket_close($sockfd);
//unset($_POST['submit']);
}
?>
<form action="tcpclient.php" method="post">
ÇëÑ¡Ôò·¢ËÍ·½Ê½£º<br><input type=text name="buf">
<input type=submit name="submit" value="·¢ËÍ">
</form>
ÕâÊÇ·þÎñÆ÷¶Ë£¨ÓÃlinux c£©
/*server.c*/
#include <string.h>
#include <stdio.h>
#include <netinet/in.h>
#include <stdlib.h>
#define SERV_PORT 123434
int main()
{
int sockfd,n,servaddr_len,client_len,connfd;
int sendd;
char * server_ip = "127.0.0.1";
char buf[80];
char send[]="hello client!\n";
struct sockaddr_in server,client;
sockfd = socket (AF_INET,SOCK_STREAM,0);
bzero(&server,sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(server_ip);
server.sin_port = htons(SERV_PORT);
servaddr_len = sizeof(server);
client_len=sizeof(client);
if((bind(sockfd,(struct sockaddr *) &server,servaddr_len))<0){
printf("bind error!\n");
exit(1);
}
if((listen(sockfd,6))<0){
printf("listen error!\n");
exit(1);
}
while(1)
{
bzero(buf,sizeof(buf));
printf("waiting for data.......\n");
connfd=accept(sockfd,(struct sockaddr *)&client,&client_len);
n = read(connfd,buf,sizeof(buf));
if(n<0){
printf("reading error\n");
break;
}
else {
printf("the data is :%s\n",buf);
sendd = write(connfd,send,strlen(send));
if(sendd<0){
printf("send to client error\n");
exit(1);
}
printf("send ok\n");
}
// write(connfd,buf,n);
close(connfd);
}
close(sockfd);
return 0;
}
ÎÒÔø³¢ÊÔÓÃudpͨÐÅ£¬udpµÄ´úÂë¿ÉÒÔ£¬µ«ÓÃÉÏÃæµÄ´úÂë½øÐÐͨÐÅ£¬Ç°Ì¨phpÒ³Ãæ×ÜÏÔʾÁ¬½Ó´íÎ󣬲»ÖªµÀÊÇʲôÔÒò£¬Çë¸÷λ¸ßÊÖÖ¸µãÒ»ÏÂ#include <string.h>
#include <stdio.h>
#include <netinet/in.h>
#include <stdlib.h>
#define SERV_PORT 123434
int main()
{
int sockfd,n,servaddr_len,client_len,connfd;
int sendd;
char * server_ip = "127.0.0.1";
char buf[80];
char send[]="hello client!\n";
struct sockaddr_in server,client;
sockfd = socket (AF_INET,SOCK_STREAM,0);
bzero(&server,sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(server_ip);
server.sin_port = htons(SERV_PORT);
servaddr_len = sizeof(server);
client_len=sizeof(client);
if((bind(sockfd,(struct sockaddr *) &server,servaddr_len))<0){
printf("bind error!\n");
exit(1);
}
if((listen(sockfd,6))<0){
printf("listen error!\n");
exit(1);
}
while(1)
{
bzero(buf,sizeof(buf));
printf("waiting for data.......\n");
connfd=accept(sockfd,(struct sockaddr *)&client,&client_len);
n = read(connfd,buf,sizeof(buf));
if(n<0){
printf("reading error\n");
break;
}
else {
printf("the data is :%s\n",buf);
sendd = write(connfd,send,strlen(send));
if(sendd<0){
printf("send to client error\n");
exit(1);
}
printf("send ok\n");
}
// write(connfd,buf,n);
close(connfd);
}
close(sockfd);
return 0;
}
¸½´ø¿ÉÐеÄudp´úÂë
udpµÄ¿Í»§¶Ë
<?php
$server_ip="127.0.0.1";
$port = 8888;
$sock=@socket_create(AF_INET,SOCK_DGRAM,0);
if(!$sock){
echo "socket create failure";
}
$buf=$_POST['buf'];
if($buf=="")
$buf="hello,how are you!\n";
if(!@socket_sendto($sock,$buf,strlen($buf),0,$server_ip,$port)){
//$sendto = socket_sendto($sock,$buf,strlen($buf),0,$server_ip,$port);
//if($sendto<0){
echo "send error\n";
socket_close($sock);
exit();
}
$buf="";
$msg="";
$recv = socket_recvfrom($sock,$msg,256,0,&$server_ip,&$port);
if($recv<0){
//if(!@socket_recvfrom($sock,$msg,256,0,&$server_ip,&$port)){
echo "recvieve error!";
socket_close($sock);
exit();
}
echo trim($msg)."\n";
socket_close($sock);
?>
<form action="udp_client.php" method="post">
<input type=text name=buf>
<input type=submit value="submit">
</form>
$server_ip="127.0.0.1";
$port = 8888;
$sock=@socket_create(AF_INET,SOCK_DGRAM,0);
if(!$sock){
echo "socket create failure";
}
$buf=$_POST['buf'];
if($buf=="")
$buf="hello,how are you!\n";
if(!@socket_sendto($sock,$buf,strlen($buf),0,$server_ip,$port)){
//$sendto = socket_sendto($sock,$buf,strlen($buf),0,$server_ip,$port);
//if($sendto<0){
echo "send error\n";
socket_close($sock);
exit();
}
$buf="";
$msg="";
$recv = socket_recvfrom($sock,$msg,256,0,&$server_ip,&$port);
if($recv<0){
//if(!@socket_recvfrom($sock,$msg,256,0,&$server_ip,&$port)){
echo "recvieve error!";
socket_close($sock);
exit();
}
echo trim($msg)."\n";
socket_close($sock);
?>
<form action="udp_client.php" method="post">
<input type=text name=buf>
<input type=submit value="submit">
</form>
udpµÄ·þÎñÆ÷¶Ë
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
main(){
int sock;
struct sockaddr_in server,client;
int recvd,snd;
int structlength;
char * server_ip = "127.0.0.1";/*server ip address*/
int port = 8888;
char recvbuf[2000],sendbuf[2000];
char str1[]="I have received:\n";
memset((char *)&server,0,sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(server_ip);
server.sin_port = htons(port);
memset((char *)&client,0,sizeof(client));
client.sin_family = AF_INET;
client.sin_addr.s_addr = htonl(INADDR_ANY);
client.sin_port = htons(port);
if((sock = socket (AF_INET,SOCK_DGRAM,0)) < 0 ){
printf("socket create error!\n");
exit(1);
}
structlength = sizeof(server);
if( bind(sock,(struct sockaddr *) &server,structlength) < 0){
printf("socket bind error!\n");
perror("bind");
exit(1);
}
while(1){
structlength = sizeof(client);
printf("waiting.......\n");
recvd = recvfrom(sock,recvbuf,sizeof(recvbuf),0,
(struct sockaddr *) & client,&structlength);
if(recvd < 0){
perror("recvfrom");
exit(EXIT_FAILURE);
}
else{
printf("received:%s\n",recvbuf);
memset(sendbuf,0,strlen(sendbuf));
memcpy(sendbuf,str1,strlen(str1));
snd = sendto(sock,sendbuf,strlen(str1),0,
(struct sockaddr *) &client,structlength);
if(snd < 0){
perror("sendto");
exit(1);
}
printf("sendok!\n");
}
}
close(sock);
}
Õâ¸öudpµÄͨÐÅÊÇ¿ÉÔËÐеÄ#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
main(){
int sock;
struct sockaddr_in server,client;
int recvd,snd;
int structlength;
char * server_ip = "127.0.0.1";/*server ip address*/
int port = 8888;
char recvbuf[2000],sendbuf[2000];
char str1[]="I have received:\n";
memset((char *)&server,0,sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(server_ip);
server.sin_port = htons(port);
memset((char *)&client,0,sizeof(client));
client.sin_family = AF_INET;
client.sin_addr.s_addr = htonl(INADDR_ANY);
client.sin_port = htons(port);
if((sock = socket (AF_INET,SOCK_DGRAM,0)) < 0 ){
printf("socket create error!\n");
exit(1);
}
structlength = sizeof(server);
if( bind(sock,(struct sockaddr *) &server,structlength) < 0){
printf("socket bind error!\n");
perror("bind");
exit(1);
}
while(1){
structlength = sizeof(client);
printf("waiting.......\n");
recvd = recvfrom(sock,recvbuf,sizeof(recvbuf),0,
(struct sockaddr *) & client,&structlength);
if(recvd < 0){
perror("recvfrom");
exit(EXIT_FAILURE);
}
else{
printf("received:%s\n",recvbuf);
memset(sendbuf,0,strlen(sendbuf));
memcpy(sendbuf,str1,strlen(str1));
snd = sendto(sock,sendbuf,strlen(str1),0,
(struct sockaddr *) &client,structlength);
if(snd < 0){
perror("sendto");
exit(1);
}
printf("sendok!\n");
}
}
close(sock);
}