socket代碼

客戶端:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
int main(int argc,char *argv[])
{int sockfd,numbytes;char buf[100];struct sockaddr_in their_addr;//int i = 0;//將基本名字和地址轉換//he = gethostbyname(argv[1]);//建立一個TCP套接口if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1){   perror("socket");printf("create socket error.建立一個TCP套接口失敗");exit(1);}   //初始化結構體,連接到服務器的2323端口their_addr.sin_family = AF_INET;their_addr.sin_port = htons(2323);// their_addr.sin_addr = *((struct in_addr *)he->h_addr);inet_aton( "127.0.0.1", &their_addr.sin_addr );bzero(&(their_addr.sin_zero),8);//和服務器建立連接if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr))==-1){   perror("connect");exit(1);}   //向服務器發送數據if(send(sockfd,"hello!socket.",6,0)==-1){perror("send");exit(1);}//接受從服務器返回的信息if((numbytes = recv(sockfd,buf,100,0))==-1){perror("recv");exit(1);}buf[numbytes] = '\0';printf("Recive from server:%s\n",buf);//關閉socket
        close(sockfd);return 0;
}
服務器端:#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <errno.h>int main()
{int sockfd,new_fd;struct sockaddr_in my_addr;struct sockaddr_in their_addr;int sin_size;//建立TCP套接口if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1){   printf("create socket error");perror("socket");exit(1);}   //初始化結構體,并綁定2323端口my_addr.sin_family = AF_INET;my_addr.sin_port = htons(2323);my_addr.sin_addr.s_addr = INADDR_ANY;bzero(&(my_addr.sin_zero),8);//綁定套接口if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1){   perror("bind socket error");exit(1);}   //創建監聽套接口if(listen(sockfd,10)==-1){perror("listen");exit(1);}//等待連接while(1){sin_size = sizeof(struct sockaddr_in);printf("server is run.\n");//如果建立連接,將產生一個全新的套接字if((new_fd = accept(sockfd,(struct sockaddr *)&their_addr,&sin_size))==-1){perror("accept");exit(1);}printf("accept success.\n");//生成一個子進程來完成和客戶端的會話,父進程繼續監聽if(!fork()){printf("create new thred success.\n");//讀取客戶端發來的信息int numbytes;char buff[256];memset(buff,0,256);if((numbytes = recv(new_fd,buff,sizeof(buff),0))==-1){perror("recv");exit(1);}printf("%s",buff);//將從客戶端接收到的信息再發回客戶端if(send(new_fd,buff,strlen(buff),0)==-1)perror("send");close(new_fd);exit(0);}close(new_fd);}close(sockfd);
}

轉載于:https://www.cnblogs.com/fll369/archive/2012/10/15/2724604.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379224.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379224.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379224.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

棧應用_將算式轉成按運算符優先級分布(代碼、分析、匯編)

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;匯編&#xff1a;代碼&#xff1a; LinkList.h LinkList.c LinkStack.h LinkStack.c 棧-線性表 main.c #include <stdio.h> #include "LinkStack.h"/* 該程序將 正常的算式 轉換成按照運算符優先分布的算式…

課堂筆記(一)

1&#xff0c;怎樣查詢函數的用法 help(函數名) 2&#xff0c;表達式float(0b1100010101)float(0o1425)float(0x315)的結果是什么&#xff0c;并說明原因 True 浮點類型的數用二進制八進制十六進制的不同表達 3&#xff0c;oct()方法 轉換八進制輸出 4&#xff0c;hex()方…

Struts2.0標簽使用之s:checkboxlist/

jsp代碼如下&#xff1a; <s:form action"receive.action" method"post"> <s:checkboxlist id"user" name"cheuser" list"#request.userlist" listKey"id" listValue"name" lab…

[轉]深入淺出Java設計模式之備忘錄模式

本文轉自&#xff1a;http://dev.yesky.com/450/2070450.shtml 一、引子   俗話說&#xff1a;世上難買后悔藥。所以凡事講究個“三思而后行”&#xff0c;但總常見有人做“痛心疾首”狀&#xff1a;當初我要是……。如果真的有《大話西游》中能時光倒流的“月光寶盒”&#…

遞歸問題(代碼、分析、匯編)

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;匯編&#xff1a;代碼&#xff1a; main.c #include <stdio.h>//該程序使用遞歸將字符串從后往前依次輸出void reverse(char* s) {if( (s ! NULL) && (*s ! \0) ){reverse(s 1);printf("%c", *s);…

Java LocalDate類| ofYearDay()方法與示例

LocalDate類的YearDay()方法 (LocalDate Class ofYearDay() method) ofYearDay() method is available in java.time package. ofYearDay()方法在java.time包中可用。 ofYearDay() method is used to create an instance of LocalDate object that holds the value from the ye…

ASP.NET C#讀寫Cookie的方法!

Cookie (HttpCookie的實例)提供了一種在 Web 應用程序中存儲用戶特定信息的方法。例如&#xff0c;當用戶訪問您的站點時&#xff0c;您可以使用 Cookie 存儲用戶首選項或其他信息。當該用戶再次訪問您的網站時&#xff0c;應用程序便可以檢索以前存儲的信息。 創建Cookie方法…

遞歸-裴波那契數列(代碼、分析、匯編)

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;匯編&#xff1a;代碼&#xff1a; main.c #include <stdio.h>//該程序輸出裴波那契數列 int fibonacci(int n) {if( n > 1 ){return fibonacci(n-1) fibonacci(n-2);//注意&#xff1a;這里調用是一直調用左邊函…

javascript 事件委派

javascript 模擬用戶操作 <a href"javascript:;" onClick"javascript:alert(131231);" id"abc">asdfasdf</a> <script> if(document.all) { document.getElementById(abc).fireEvent(onclick); } else { var evt document.cr…

Java Duration類| isNegative()方法與示例

持續時間類isNegative()方法 (Duration Class isNegative() method) isNegative() method is available in java.time package. isNegative()方法在java.time包中可用。 isNegative() method is used to check whether this Duration object holds the value of length < 0 …

經典例題(一)

1&#xff0c;已知復數 x 6 8j 請寫出它的模、實部、虛部及共軛復數的命令&#xff0c;并寫出運行結果。 X 6 8j print("模為:%d"% abs(X)) print("實部為:%s"% X.real) print("虛部為:%s"% X.imag) print("共軛復數為:%s"% X.co…

asterisk撥號規則

一、前言 本文檔以asterisk-1.4.32為基礎寫作而成&#xff0c;可能和其他版本有些區別。其中參考了一些別的書籍和文章。因為寫的比較倉促&#xff0c;而且基本都是晚上寫的&#xff0c;里面的內容邏輯性和語句沒有仔細斟酌&#xff0c;就是想到什么寫什么&#xff0c;難免有…

getseconds補0_Java Duration類| getSeconds()方法與示例

getseconds補0持續時間類getSeconds()方法 (Duration Class getSeconds() method) getSeconds() method is available in java.time package. getSeconds()方法在java.time包中可用。 getSeconds() method is used to return the number of seconds exists in this Duration. g…

遞歸-漢諾塔(代碼、分析、匯編)

代碼&#xff1a; #include <stdio.h>void hanoi(int n, char a, char b, char c) {if( n > 0 ){if( n 1 ){printf("%c -> %c\n", a, c);}else{hanoi(n-1, a, c, b);printf("%c -> %c\n", a, c);hanoi(n-1, b, a, c);}} }int main() {han…

if語句(四)

1&#xff0c;簡單if示例 phones [iphone,xiaomi,huawei,smartisan] for phone in phones:if phone huawei:print(phone.upper())#將字符串的所有字母大寫else:print(phone.title())#將字符串中的每個單詞的首字符大寫效果圖如下&#xff1a; 2&#xff0c;if條件測試 ph…

welcome to my blog

轉載于:https://www.cnblogs.com/jiangjun/archive/2012/10/22/2734600.html

kotlin字符串判空_Kotlin程序檢查空字符串,空字符串或NULL字符串

kotlin字符串判空Given a string, we have to check whether it is an empty, blank or NULL string. 給定一個字符串&#xff0c;我們必須檢查它是否為空&#xff0c;空白或NULL字符串。 Example: 例&#xff1a; Input:str ""Output:True用于在Kotlin中檢查Empt…

.net中對象序列化技術淺談 (轉)

原文&#xff1a;http://blog.csdn.net/zhoufoxcn/archive/2009/03/11/3978874.aspx .net中對象序列化技術淺談 (轉&#xff09;序列化是將對象狀態轉換為可保持或傳輸的格式的過程。與序列化相對的是反序列化&#xff0c;它將流轉換為對象。這兩個過程結合起來&#xff0c;可…

遞歸-輸出字符串所有的組合情況(代碼、分析、匯編)

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;匯編&#xff1a;代碼&#xff1a; #include <stdio.h>/*程序描述&#xff1a;輸出字符串所有的組合情況使用permutation函數進行將指定的下標值&#xff0c;與最大下標值這個范圍的每個下標值進行交換每調用一次permu…

課本例子代碼第四章

【例4.1】設計一個控制臺應用程序&#xff0c;采用二分查找方法在給定的有序數組a中查找用戶輸入的值&#xff0c;并提示相應的查找結果。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace C…