導航:首頁 > 編程語言 > linux網路編程send

linux網路編程send

發布時間:2024-10-13 02:13:55

linux闃誨炰笌闈為樆濉瀕inux闃誨為潪闃誨

鍦ㄩ潪闃誨炴ā寮忎笂鎬庝箞鐭ラ亾recv鎺ユ敹鏁版嵁瀹屾垚錛

浠linux涓媡cpsocket緙栫▼涓轟緥錛氶樆濉炲氨鏄痳ecv/read鐨勬椂鍊檚ocket鎺ユ敹緙撳啿鍖鴻佹槸鏈夋暟鎹灝辮伙紝娌℃暟鎹鎴戝氨涓鐩寸潯瑙夎禆鐫涓嶈蛋錛岀洿鍒版湁鏁版嵁鏉ヤ簡璇誨畬鎴戞墠璧般

send/write鐨勬椂鍊欙紝瑕佹槸鍙戦佺紦鍐插尯婊′簡錛屾病鏈夌┖闂寸戶緇鍙戦佷簡鎴戜篃涓鐩寸潯瑙夎禆鐫涓嶈蛋錛岀洿鍒板彂閫佺紦鍐插尯鑵懼嚭瓚沖熺殑絀洪棿璁╂垜鎶婃暟鎹鍏ㄩ儴濉炲埌鍙戦佺紦鍐插尯閲屾垜鎵嶈蛋銆

錛堝綋鐒跺傛灉浣犻氳繃setsockopt璁劇疆浜嗚誨啓瓚呮椂錛岃秴鏃舵椂闂村埌浜嗚繕鏄浼氳繑鍥-1鍜孍AGAIN錛屼笉鍐嶇潯瑙夌瓑寰咃級

闈為樆濉炲氨鏄痳ecv/read鐨勬椂鍊欙紝瑕佹槸鎺ユ敹緙撳啿鍖烘湁鏁版嵁鎴戝氨璇誨畬錛屾病鏈夋暟鎹鎴戠洿鎺ュ甫鐫榪斿洖鐨-1鍜孍GAIN璧頒漢錛岀粷涓嶇潯瑙夌瓑寰呰借鏃墮棿銆

write/send鐨勬椂鍊欙紝瑕佹槸鍙戦佺紦鍐插尯鏈夎凍澶熺殑絀洪棿錛屽氨絝嬪埢鎶婃暟鎹濉炲埌鍙戦佺紦鍐插尯鍘伙紝鐒跺悗璧頒漢錛屽傛灉鍙戦佺紦瀛樺尯婊′簡錛岀┖闂翠笉瓚籌紝閭g洿鎺ュ甫鐫榪斿洖鐨-1鍜孍AGAIN璧頒漢銆傝嚦浜嶪O澶氳礬澶嶇敤錛岄栧厛瑕佺悊瑙g殑鏄錛屾搷浣滅郴緇熶負浣犳彁渚涗簡涓涓鍔熻兘錛屽綋浣犵殑鏌愪釜socket鎺ユ敹緙撳瓨鍖烘湁鏁版嵁鍙璇伙紝鎴栬呭彂閫佺紦鍐插尯鏈夌┖闂村彲鍐欑殑鏃跺欙紝瀹冨彲浠ョ粰浣犱竴涓閫氱煡銆

榪欐牱褰撻厤鍚堥潪闃誨炵殑socket浣跨敤鏃訛紝鍙鏈夊綋緋葷粺閫氱煡鎴戝摢涓鎻忚堪絎﹀彲璇諱簡錛屾垜鎵嶅幓鎵ц宺ead鎿嶄綔錛屽彲浠ヤ繚璇佹瘡嬈read閮借兘璇誨埌鏈夋晥鏁版嵁鑰屼笉鍋氱函榪斿洖-1鍜孍AGAIN鐨勬棤鐢ㄥ姛銆

鍐欐搷浣滅被浼箋

鎿嶄綔緋葷粺鐨勮繖涓鍔熻兘閫氳繃select/poll/epoll涔嬬被鐨勭郴緇熻皟鐢ㄥ嚱鏁版潵浣跨敤錛岃繖浜涘嚱鏁伴兘鍙浠ュ悓鏃剁洃瑙嗗氫釜鎻忚堪絎︾殑璇誨啓灝辯華鐘跺喌錛岃繖鏍鳳紝澶氫釜鎻忚堪絎︾殑I/O鎿嶄綔閮借兘鍦ㄤ竴涓綰跨▼鍐呭畬鎴愶紝榪欏氨鍙獻/O澶氳礬澶嶇敤錛岃繖閲岀殑鈥滃嶇敤鈥濇寚鐨勬槸澶嶇敤鍚屼竴涓綰跨▼銆傝嚦浜庝簨浠墮┍鍔錛屽叾瀹炴槸I/O澶氳礬澶嶇敤鐨勪竴涓鍙﹀栫殑縐板懠銆傝嚦浜庡紓姝ュ悓姝ワ紝鎴戜滑甯歌佺殑linux涓嬬殑緗戠粶緙栫▼妯″瀷澶ч儴鍒嗛兘鏄鍚屾io錛屼互璇繪搷浣滀負渚嬶紝鏈璐ㄤ笂閮芥槸闇瑕佺敤鎴瘋皟鐢╮ead/recv鍘諱粠鍐呮牳緙撳啿鍖烘妸鏁版嵁璇誨畬鍐嶅勭悊涓氬姟閫昏緫銆

寮傛io鍒欐槸鍐呮牳宸茬粡鎶婃暟鎹璇誨ソ浜嗭紝鐢ㄦ埛鐩存帴澶勭悊閫昏緫銆

寮傛IO鍦╨inux涓嬩竴鑸鏄鐢╝io搴撱

linuxaccept()鍑芥暟涓鐩村勪簬闃誨炵姸鎬,浠涔堝師鍥狅紵

accept錛堬級鍑芥暟灝辨槸闃誨炵殑鍟婏紝瑕佺瓑寰呮帴鏀跺埌鏈夊㈡埛絝璇鋒眰鎵嶅彲浠ヨ繘琛屽悗緇鐨勬搷浣滐紝浣犳墍璋撶殑涓嶅彲浠ユ槸鎸囦粈涔堬紵

open緋葷粺浣跨敤鎶宸э紵

open緋葷粺鏄澶氱嶈璦銆佺幆澧冪殑涓縐嶅嚱鏁般

LINUX涓璷pen鍑芥暟浣滅敤錛氭墦寮鍜屽壋寤烘枃浠躲侾B紼嬪簭璇璦涓璷pen鍔熻兘錛氭墦寮紿楀彛銆

瀵逛簬open緋葷粺鏉ヨ達紝絎涓変釜鍙傛暟浠呭綋鍒涘緩鏂版枃浠舵椂錛堝嵆浣跨敤浜哋_CREAT鏃訛級鎵嶄嬌鐢錛岀敤浜庢寚瀹氭枃浠剁殑璁塊棶鏉冮檺浣嶃

pathname鏄寰呮墦寮/鍒涘緩鏂囦歡鐨凱OSIX璺寰勫悕錛沠lags鐢ㄤ簬鎸囧畾鏂囦歡鐨勬墦寮/鍒涘緩妯″紡錛岃繖涓鍙傛暟鍙鐢變互涓嬪父閲忛氳繃閫昏緫浣嶆垨閫昏緫鏋勬垚銆

鎵撳紑/鍒涘緩鏂囦歡鏃訛紝鑷沖皯寰椾嬌鐢ㄤ笂榪頒笁涓甯擱噺涓鐨勪竴涓銆

浠ヤ笅甯擱噺鏄閫夌敤鐨勶細

1.璁劇疆涓洪潪闃誨炴ā寮

2.浠ヤ笅涓変釜甯擱噺鍚屾牱鏄閫夌敤鐨勶紝瀹冧滑鐢ㄤ簬鍚屾ヨ緭鍏ヨ緭鍑

3.open榪斿洖鐨勬枃浠舵弿榪扮︿竴瀹氭槸鏈灝忕殑鏈琚浣跨敤鐨勬弿榪扮︺

❷ linux下socket 網路編程(客戶端向伺服器端發送文件) 求源代碼 大哥大姐幫幫忙 。。謝謝

server:

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <syslog.h>
#include <sys/time.h>
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>

#define MAXDATASIZE 1024
#define SERVPORT 19999
#define BACKLOG 10

int SendFileToServ(const char *path, const char *FileName, const char *ip)
{
#define PORT 20002
int sockfd;
int recvbytes;
char buf[MAXDATASIZE];
char send_str[MAXDATASIZE];
char filepath[128] = {0};
struct sockaddr_in serv_addr;
FILE *fp;
sprintf(filepath, "%s%s", path, FileName);

if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
return 1;
}
bzero(&serv_addr,sizeof(struct sockaddr_in));
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(PORT);
inet_aton(ip, &serv_addr.sin_addr);
int IErrCount = 0;
again:
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1)
{
if (5 == IErrCount)
return 1;
IErrCount++;
perror("connect");
sleep(2);
goto again;
}
//if ((fp = fopen(FileName, "rb")) == NULL)
if ((fp = fopen(filepath, "rb")) == NULL)
{
perror("fopen ");
return 1;
}
recvbytes = write(sockfd, FileName, strlen(FileName));
recvbytes = read(sockfd, buf, MAXDATASIZE);
if (!memcmp(buf, "sendmsg", 7))
{
while(fgets(send_str, MAXDATASIZE, fp))
{
recvbytes = write(sockfd, send_str, strlen(send_str));
recvbytes = read(sockfd, buf, MAXDATASIZE);
if (recvbytes <= 0)
{
fclose(fp);
close(sockfd);
return 1;
}
if (memcmp(buf, "goon", 4))
{
fclose(fp);
close(sockfd);
return 1;
}
}
recvbytes = write(sockfd, "end", 3);
}
else
{
fclose(fp);
close(sockfd);
return 1;
}
memset(buf, 0, MAXDATASIZE);
if (read(sockfd, buf, MAXDATASIZE) <= 0)
{
close(sockfd);
return 2;
}
char *Eptr = "nginx reload error";
//printf("bf[%s]\n", buf);
int ret;
ret = strncmp(buf, Eptr, strlen(Eptr));
//printf("%d\n", ret);
if (!ret)
{
close(sockfd);
return 2;
}

close(sockfd);
return 0;
}

int mysyslog(const char * msg)
{
FILE *fp;
if ((fp = fopen("/tmp/tmp.log", "a+")) == NULL)
{
return 0;
}
fprintf(fp, "[%s]\n", msg);
fclose(fp);
return 0;
}
static void quit_handler(int signal)
{
kill(0, SIGUSR2);
syslog( LOG_NOTICE, "apuserv quit...");
// do something exit thing ,such as close socket ,close mysql,free list
// .....
//i end
exit(0);
}
static int re_conf = 0;
static void reconf_handler(int signal)
{
re_conf=1;
syslog(LOG_NOTICE,"apuserv reload configure file .");
// 請在循環體中判斷,如果re_conf == 1,請再次載入配置文件。
}
static int isrunning(void)
{
int fd;
int ret;
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = 0;
lock.l_start = 0;
lock.l_len = 0;
const char *lckfile = "/tmp/apuserv.lock";
fd = open(lckfile,O_WRONLY|O_CREAT);
if (fd < 0) {
syslog(LOG_ERR,"can not create lock file: %s\n",lckfile);
return 1;
}
if ((ret = fcntl(fd,F_SETLK,&lock)) < 0) {
ret = fcntl(fd,F_GETLK,&lock);
if (lock.l_type != F_UNLCK) {
close(fd);
return lock.l_pid;
}
else {
fcntl(fd,F_SETLK,&lock);
}
}
return 0;
}

int MyHandleBuff(const char *buf, char *str, char *FileName, char *pth)
{
sscanf(buf, "%s %s %s", pth, FileName, str);
printf("path=%s\nfilename=%s\nip=%s\n", pth, FileName, str);
return 0;
}

int main(int argc, char **argv)
{
int sockfd,client_fd;
socklen_t sin_size;
struct sockaddr_in my_addr,remote_addr;
char buff[MAXDATASIZE];
int recvbytes;
#if 1
int pid ;
char ch ;
int ret;
int debug = 0;
signal(SIGUSR1, SIG_IGN);
signal(SIGUSR2, SIG_IGN);
signal(SIGHUP, SIG_IGN);
signal(SIGTERM, quit_handler);
syslog(LOG_NOTICE,"apuserver start....");
while ((ch = getopt(argc, argv, "dhV")) != -1) {
switch (ch) {
case 'd':
debug = 1;
break;
case 'V':
printf("Version:%s\n","1.0.0");
return 0;
case 'h':
printf(" -d use daemon mode\n");
printf(" -V show version\n");
return 0;
default:
printf(" -d use daemon mode\n");
printf(" -V show version\n");
}
}
if (debug && daemon(0,0 ) ) {
return -1;
}
if (isrunning()) {
fprintf(stderr, "apuserv is already running\n");
syslog(LOG_INFO,"apuserv is already running\n");
exit(0);
}
while (1) {
pid = fork();
if (pid < 0)
return -1;
if (pid == 0)
break;
while ((ret = waitpid(pid, NULL, 0)) != pid) {
syslog(LOG_NOTICE, "waitpid want %d, but got %d", pid, ret);
if (ret < 0)
syslog(LOG_NOTICE, "waitpid errno:%d", errno);
}
kill(0, SIGUSR2);
sleep(1);
syslog(LOG_NOTICE,"restart apuserver");
}
signal(SIGHUP, reconf_handler);
signal(SIGPIPE, SIG_IGN);
signal(SIGUSR1,SIG_IGN);
signal(SIGUSR2, SIG_DFL);
signal(SIGTERM, SIG_DFL);
#endif
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}

bzero(&my_addr,sizeof(struct sockaddr_in));
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{
perror("bind");
exit(1);
}
if(listen(sockfd,BACKLOG)==-1)
{
perror("listen");
exit(1);
}
int nret;
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size))==-1)
{
perror("falied accept");
continue;
}
memset(buff, 0, MAXDATASIZE);
recvbytes = read(client_fd, buff, MAXDATASIZE);
char str[16] = {0};
char FileName[128] = {0};
char path[128] = {0};
MyHandleBuff(buff, str, FileName, path);
if (recvbytes > 0)
{
nret = SendFileToServ(path, FileName, str);
printf("nret[%d]\n", nret);
if (1 == nret)
write(client_fd, "send file error", 15);
else if(2 == nret)
write(client_fd, "reload nginx error", 18);
else
write(client_fd, "succ", 4);
}
close(client_fd);
}
}
_________________________________________________
client:
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <syslog.h>
#include <sys/time.h>
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>

#define MAXDATASIZE 1024
#define SERVPORT 20002
#define BACKLOG 10

int mysyslog(const char * msg)
{
FILE *fp;
if ((fp = fopen("/tmp/tmp.log", "a+")) == NULL)
{
return 0;
}
fprintf(fp, "[%s]\n", msg);
fclose(fp);
return 0;
}
static void quit_handler(int signal)
{
kill(0, SIGUSR2);
syslog( LOG_NOTICE, "apuserv quit...");
// do something exit thing ,such as close socket ,close mysql,free list
// .....
//i end
exit(0);
}
static int re_conf = 0;
static void reconf_handler(int signal)
{
re_conf=1;
syslog(LOG_NOTICE,"apuserv reload configure file .");
// ????·???????1nf == 1£???′μ?????

static int isrunning(void)
{
int fd;
int ret;
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = 0;
lock.l_start = 0;
lock.l_len = 0;
const char *lckfile = "/tmp/dstserver.lock";
fd = open(lckfile,O_WRONLY|O_CREAT);
if (fd < 0) {
syslog(LOG_ERR,"can not create lock file: %s\n",lckfile);
return 1;
}
if ((ret = fcntl(fd,F_SETLK,&lock)) < 0) {
ret = fcntl(fd,F_GETLK,&lock);
if (lock.l_type != F_UNLCK) {
close(fd);
return lock.l_pid;
}
else {
fcntl(fd,F_SETLK,&lock);
}
}
return 0;
}

int main(int argc, char **argv)
{
int sockfd,client_fd;
socklen_t sin_size;
struct sockaddr_in my_addr,remote_addr;
char buff[MAXDATASIZE];
int recvbytes;
#if 1
int pid ;
char ch ;
int ret;
int debug = 0;
signal(SIGUSR1, SIG_IGN);
signal(SIGUSR2, SIG_IGN);
signal(SIGHUP, SIG_IGN);
signal(SIGTERM, quit_handler);
syslog(LOG_NOTICE,"dstserver start....");

while ((ch = getopt(argc, argv, "dhV")) != -1) {
switch (ch) {
case 'd':
debug = 1;
break;
case 'V':
printf("Version:%s\n","1.0.0");
return 0;
case 'h':
printf(" -d use daemon mode\n");
printf(" -V show version\n");
return 0;
default:
printf(" -d use daemon mode\n");
printf(" -V show version\n");
}
}
if (debug && daemon(0,0 ) ) {
return -1;
}
if (isrunning()) {
fprintf(stderr, "dstserver is already running\n");
syslog(LOG_INFO,"dstserver is already running\n");
exit(0);
}
while (1) {
pid = fork();
if (pid < 0)
return -1;
if (pid == 0)
break;
while ((ret = waitpid(pid, NULL, 0)) != pid) {
syslog(LOG_NOTICE, "waitpid want %d, but got %d", pid, ret);
if (ret < 0)
syslog(LOG_NOTICE, "waitpid errno:%d", errno);
}
kill(0, SIGUSR2);
sleep(1);
syslog(LOG_NOTICE,"restart apuserver");
}
signal(SIGHUP, reconf_handler);
signal(SIGPIPE, SIG_IGN);
signal(SIGUSR1,SIG_IGN);
signal(SIGUSR2, SIG_DFL);
signal(SIGTERM, SIG_DFL);
#endif
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}

bzero(&my_addr,sizeof(struct sockaddr_in));
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{
perror("bind");
exit(1);
}
if(listen(sockfd,BACKLOG)==-1)
{
perror("listen");
exit(1);
}

char filepath[MAXDATASIZE]= {0};
FILE *fp;
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size))==-1)
{
perror("falied accept");
continue;
}
memset(buff, 0, MAXDATASIZE);
recvbytes = read(client_fd, buff, MAXDATASIZE);
sprintf(filepath, "/etc/nginx/url_rule/%s", buff);
if ((fp = fopen(filepath, "wb")) == NULL)
{
perror("fopen");
close(client_fd);
continue;
}
write(client_fd, "sendmsg", 7);
while(read(client_fd, buff, MAXDATASIZE))
{
if (!memcmp(buff, "end", 3))
{
fclose(fp);
break;
}
else
{
fprintf(fp, "%s", buff);
write(client_fd, "goon", 4);
}
}
//system("nginx -s reload");
char *Sptr = "nginx reload succ";
char *Eptr = "nginx reload error";
int ret;
ret = system("nginx -s reload");
printf("ret[%d]\n", ret);
if (ret != 0)
{
write(client_fd, Eptr, strlen(Eptr));
}
else
{
write(client_fd, Sptr, strlen(Sptr));
}
close(client_fd);
}
}

以前寫的:內容忘記了。不是很復雜你可以自己看!

❸ linux 網路編程 send 做了什麼操作

send解析
sockfd:指定發送端套接字描述符。
buff: 存放要發送數據的緩沖區
nbytes: 實際要改善的數據的位元組數
flags: 一般設置為0
1) send先比較發送數據的長度nbytes和套接字sockfd的發送緩沖區的長度,如果nbytes > 套接字sockfd的發送緩沖區的長度, 該函數返回SOCKET_ERROR;
2) 如果nbtyes <= 套接字sockfd的發送緩沖區的長度,那麼send先檢查協議是否正在發送sockfd的發送緩沖區中的數據,如果是就等待協議把數據發送完,如果協議還沒有開始發送sockfd的發送緩沖區中的數據或者sockfd的發送緩沖區中沒有數據,那麼send就比較sockfd的發送緩沖區的剩餘空間和nbytes
3) 如果 nbytes > 套接字sockfd的發送緩沖區剩餘空間的長度,send就一起等待協議把套接字sockfd的發送緩沖區中的數據發送完
4) 如果 nbytes < 套接字sockfd的發送緩沖區剩餘空間大小,send就僅僅把buf中的數據到剩餘空間里(注意並不是send把套接字sockfd的發送緩沖區中的數據傳到連接的另一端的,而是協議傳送的,send僅僅是把buf中的數據到套接字sockfd的發送緩沖區的剩餘空間里)。
5) 如果send函數成功,就返回實際的位元組數,如果send在數據時出現錯誤,那麼send就返回SOCKET_ERROR; 如果在等待協議傳送數據時網路斷開,send函數也返回SOCKET_ERROR。
6) send函數把buff中的數據成功到sockfd的改善緩沖區的剩餘空間後它就返回了,但是此時這些數據並不一定馬上被傳到連接的另一端。如果協議在後續的傳送過程中出現網路錯誤的話,那麼下一個socket函數就會返回SOCKET_ERROR。(每一個除send的socket函數在執行的最開始總要先等待套接字的發送緩沖區中的數據被協議傳遞完畢才能繼續,如果在等待時出現網路錯誤那麼該socket函數就返回SOCKET_ERROR)
7) 在unix系統下,如果send在等待協議傳送數據時網路斷開,調用send的進程會接收到一個SIGPIPE信號,進程對該信號的處理是進程終止。

❹ linux下socket 網路編程(客戶端向伺服器端發送文件) 求源代碼 大哥大姐幫幫忙 ,。。謝謝

源代碼奉上,流程圖。。。這個太簡單了,你自己看看。。。。。。。

//TCP
//伺服器端程序
#include< stdio.h >
#include< stdlib.h >
#include< windows.h >
#include< winsock.h >
#include< string.h >

#pragma comment( lib, "ws2_32.lib" )

#define PORT 2046
#define BACKLOG 10
#define TRUE 1

void main( void )
{
int iServerSock;
int iClientSock;

char *buf = "hello, world!\n";

struct sockaddr_in ServerAddr;
struct sockaddr_in ClientAddr;

int sin_size;

WSADATA WSAData;

if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )//初始化
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}

if( ( iServerSock = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET )
{
printf( "創建套接字失敗!\n" );
WSACleanup( );
exit( 0 );
}

ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );//監視的埠號
ServerAddr.sin_addr.s_addr = INADDR_ANY;//本地IP
memset( & ( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );

if( bind( iServerSock, ( struct sockaddr * )&ServerAddr, sizeof( struct sockaddr ) ) == -1 )
{
printf( "bind調用失敗!\n" );
WSACleanup( );
exit( 0 );
}

if( listen( iServerSock, BACKLOG ) == -1 )
{
printf( "listen調用失敗!\n" );
WSACleanup( );
exit( 0 );
}

while( TRUE )
{
sin_size = sizeof( struct sockaddr_in );
iClientSock = accept( iServerSock, ( struct sockaddr * )&ClientAddr, &sin_size );

if( iClientSock == -1 )
{
printf( "accept調用失敗!\n" );
WSACleanup( );
exit( 0 );
}

printf( "伺服器連接到%s\n", inet_ntoa( ClientAddr.sin_addr ) );
if( send( iClientSock, buf, strlen( buf ), 0 ) == -1 )
{
printf( "send調用失敗!" );
closesocket( iClientSock );
WSACleanup( );
exit( 0 );
}
}
}

/////客戶端程序
#include< stdio.h >
#include< stdlib.h >
#include< windows.h >
#include< winsock.h >
#include< string.h >

#pragma comment( lib, "ws2_32.lib" )

#define PORT 2046
#define BACKLOG 10
#define TRUE 1
#define MAXDATASIZE 100

void main( void )
{

int iClientSock;
char buf[ MAXDATASIZE ];
struct sockaddr_in ServerAddr;
int numbytes;
// struct hostent *he;
WSADATA WSAData;

// int sin_size;

/* if( ( he = gethostbyname( "liuys" ) ) == NULL )
{
printf( "gethostbyname調用失敗!" );
WSACleanup( );
exit( 0 );
}
*/

if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )//初始化
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}

if( ( iClientSock = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET )
{
printf( "創建套接字失敗!\n" );
WSACleanup( );
exit( 0 );
}

ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );
// ServerAddr.sin_addr = *( ( struct in_addr * )he->h_addr );
ServerAddr.sin_addr.s_addr = inet_addr( "192.168.2.194" );//記得換IP
memset( &( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );

if( connect( iClientSock, ( struct sockaddr * ) & ServerAddr, sizeof( struct sockaddr ) ) == -1 )
{
printf( "connect失敗!" );
WSACleanup( );
exit( 0 );
}

numbytes = recv( iClientSock, buf, MAXDATASIZE, 0 );

if( numbytes == -1 )
{
printf( "recv失敗!" );
WSACleanup( );
exit( 0 );
}

buf[ numbytes ] = '\0';

printf( "Received: %s", buf );

closesocket( iClientSock );
WSACleanup( );
}

/////UDP
//伺服器
#include< stdio.h >
#include< string.h >
#include< winsock.h >
#include< windows.h >

#pragma comment( lib, "ws2_32.lib" )

#define PORT 2046
#define BACKLOG 10
#define TRUE 1
#define MAXDATASIZE 1000

void main( void )
{
int iServerSock;
// int iClientSock;

int addr_len;
int numbytes;

char buf[ MAXDATASIZE ];

struct sockaddr_in ServerAddr;
struct sockaddr_in ClientAddr;

WSADATA WSAData;
if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}

iServerSock = socket( AF_INET, SOCK_DGRAM, 0 );
if( iServerSock == INVALID_SOCKET )
{
printf( "創建套接字失敗!\n" );
WSACleanup( );
exit( 0 );
}

ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );//監視的埠號
ServerAddr.sin_addr.s_addr = INADDR_ANY;//本地IP
memset( & ( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );

if( bind( iServerSock, ( struct sockaddr * )&ServerAddr, sizeof( struct sockaddr ) ) == -1 )
{
printf( "bind調用失敗!\n" );
WSACleanup( );
exit( 0 );
}

addr_len = sizeof( struct sockaddr );
numbytes = recvfrom( iServerSock, buf, MAXDATASIZE, 0, ( struct sockaddr * ) & ClientAddr, &addr_len );
if( numbytes == -1 )
{
printf( "recvfrom調用失敗!\n" );
WSACleanup( );
exit( 0 );
}

printf( "got packet from %s\n", inet_ntoa( ClientAddr.sin_addr ) );
printf( "packet is %d bytes long\n", numbytes );
buf[ numbytes ] = '\0';
printf( "packet contains \"%s\"\n", buf );

closesocket( iServerSock );
WSACleanup( );

}
//客戶端
#include< stdio.h >
#include< stdlib.h >
#include< windows.h >
#include< winsock.h >
#include< string.h >

#pragma comment( lib, "ws2_32.lib" )

#define PORT 2046
#define MAXDATASIZE 100

void main( void )
{

int iClientSock;
struct sockaddr_in ServerAddr;

int numbytes;
char buf[ MAXDATASIZE ] = { 0 };

WSADATA WSAData;
if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )
{
printf( "initializationing error!\n" );
WSACleanup( );
exit( 0 );
}

if( ( iClientSock = socket( AF_INET, SOCK_DGRAM, 0 ) ) == -1 )
{
printf( "創建套接字失敗!\n" );
WSACleanup( );
exit( 0 );
}

ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( PORT );
ServerAddr.sin_addr.s_addr = inet_addr( "192.168.2.194" );//記得換IP
memset( &( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );

numbytes = sendto( iClientSock, buf, strlen( buf ), 0, ( struct sockaddr * ) & ServerAddr, sizeof( struct sockaddr ) );
if( numbytes == -1 )
{
printf( "sendto調用失敗!\n" );
WSACleanup( );
exit( 0 );
}

printf( "sent %d bytes to %s\n", numbytes, inet_ntoa( ServerAddr.sin_addr ) );

closesocket( iClientSock );
WSACleanup( );
}

閱讀全文

與linux網路編程send相關的資料

熱點內容
湖北黃石dns伺服器雲主機 瀏覽:581
奇異博士是個什麼app 瀏覽:259
單片機數碼管閃爍 瀏覽:953
有什麼閱讀小說免費的app 瀏覽:661
華潤電力是個什麼app 瀏覽:372
java與jni線程 瀏覽:202
c語言哈夫曼編碼壓縮 瀏覽:27
單片機零基礎入門 瀏覽:437
千鋒python3基礎視頻 瀏覽:578
linuxintel程序 瀏覽:977
光遇登陸伺服器連接失敗什麼意思 瀏覽:767
jd檢測手機新老介面源碼 瀏覽:126
java調用interface 瀏覽:1
解決點面之間是否存在聯系的演算法 瀏覽:272
如何架構雲伺服器 瀏覽:395
象山證券交易用什麼app 瀏覽:776
量價分析pdf 瀏覽:714
php如何調用js方法 瀏覽:743
網頁游戲在哪個文件夾 瀏覽:689
淘特怎麼付不了款伺服器出錯了 瀏覽:115