| ÍøÕ¾Ê×Ò³ | Òµ½çÐÂÎÅ | С×é | Íþ¿Í | È˲Š| ÏÂÔØƵµÀ | ²©¿Í | ´úÂëÌù | ÔÚÏß±à³Ì | ±à³ÌÂÛ̳
»¶Ó­¼ÓÈëÎÒÃÇ£¬Ò»Í¬Çд輼Êõ
Óû§Ãû£º   
 
ÃÜ¡¡Â룺  
¹²ÓÐ 791 È˹Ø×¢¹ý±¾Ìû
±êÌ⣺Ϊʲôǰ̨ºÍºǫ́²»ÄÜͨÐÅ£¿
Ö»¿´Â¥Ö÷ ¼ÓÈëÊÕ²Ø
pipiku
Rank: 1
µÈ¡¡¼¶£ºÐÂÊÖÉÏ·
Ìû¡¡×Ó£º49
ר¼Ò·Ö£º2
×¢¡¡²á£º2009-3-20
½áÌûÂÊ£º87.5%
ÊÕ²Ø
 ÎÊÌâµãÊý£º0 »Ø¸´´ÎÊý£º1 
Ϊʲôǰ̨ºÍºǫ́²»ÄÜͨÐÅ£¿
ÕâÊÇ¿Í»§¶Ë£¨ÓÃ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>


ÕâÊÇ·þÎñÆ÷¶Ë£¨ÓÃ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Ò³Ãæ×ÜÏÔʾÁ¬½Ó´íÎ󣬲»ÖªµÀÊÇʲôԭÒò£¬Çë¸÷λ¸ßÊÖÖ¸µãÒ»ÏÂ
¸½´ø¿ÉÐеÄ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>


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µÄͨÐÅÊÇ¿ÉÔËÐеÄ

ËÑË÷¸ü¶àÏà¹ØÖ÷ÌâµÄÌû×Ó: ͨÐÅ¡¡
2010-12-25 16:44
gupiao175
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
µÈ¡¡¼¶£º°æÖ÷
Íþ¡¡Íû£º40
Ìû¡¡×Ó£º1787
ר¼Ò·Ö£º7527
×¢¡¡²á£º2007-6-27
ÊÕ²Ø
µÃ·Ö:0 
Õâ¸öÎÊÌâºÜÓÐÄѶȰ¡£¬¼ÈÒª¾«Í¨PHP£¬ÓÖÒª¾«Í¨C++µÄͨÐÅ֪ʶ£¬ÎÞÄÜΪÁ¦£¡

Q:1428196631£¬°Ù¶È£º¿ª·¢µØ ¼´¿ÉÕÒµ½ÎÒ,ÓÐÊÂÇëÁôÑÔ£¡
2011-01-08 22:11
¿ìËٻظ´£ºÎªÊ²Ã´Ç°Ì¨ºÍºǫ́²»ÄÜͨÐÅ£¿
Êý¾Ý¼ÓÔØÖÐ...
 
¡¡¡¡¡¡



¹ØÓÚÎÒÃÇ | ¹ã¸æºÏ×÷ | ±à³ÌÖйú | Çå³ýCookies | TOP | ÊÖ»ú°æ

±à³ÌÖйú °æȨËùÓУ¬²¢±£ÁôËùÓÐȨÀû¡£
Powered by Discuz, Processed in 0.031350 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved