谁能给个C语言socket 通信,用UDP协议的代码例子。。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了谁能给个C语言socket 通信,用UDP协议的代码例子。。相关的知识,希望对你有一定的参考价值。
使用#include<winsock2.h>库中的socket,学习学习。本人不懂,问题有疑问的话,留言!
我的是包含的#include <sys/socket.h>,参考一下吧<udp_client.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(void)
int sfd; char buf[1024]; int n, i;
struct sockaddr_in serv_addr, cli_addr;
socklen_t len;
sfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8000);
inet_pton(AF_INET, "192.168.0.36", &serv_addr.sin_addr.s_addr);
while(1)
if(fgets(buf, 1024, stdin) == NULL)
break;
sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
n = recvfrom(sfd, buf, 1024, 0, NULL, NULL);
write(STDOUT_FILENO, buf, n);
close(sfd);
return 0;
<udp_server.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(void)
int sfd; char buf[1024]; int n, i;
struct sockaddr_in serv_addr, cli_addr;
socklen_t len;
sfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8000);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(sfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
while(1)
len = sizeof(cli_addr);
n = recvfrom(sfd, buf, 1024, 0, (struct sockaddr *)&cli_addr, &len);
for(i = 0; i < n; i++)
buf[i] = toupper(buf[i]);
sendto(sfd, buf, n, 0, (struct sockaddr *)&cli_addr, len);
close(sfd);
return 0;
请求采纳,谢谢! 参考技术A #include <sys/socket.h>
<udp_client.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(void)
int sfd; char buf[1024]; int n, i;
struct sockaddr_in serv_addr, cli_addr;
socklen_t len;
sfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8000);
inet_pton(AF_INET, "192.168.0.36", &serv_addr.sin_addr.s_addr);
while(1)
if(fgets(buf, 1024, stdin) == NULL)
break;
sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
n = recvfrom(sfd, buf, 1024, 0, NULL, NULL);
write(STDOUT_FILENO, buf, n);
close(sfd);
return 0;
<udp_server.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(void)
int sfd; char buf[1024]; int n, i;
struct sockaddr_in serv_addr, cli_addr;
socklen_t len;
sfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8000);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(sfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
while(1)
len = sizeof(cli_addr);
n = recvfrom(sfd, buf, 1024, 0, (struct sockaddr *)&cli_addr, &len);
for(i = 0; i < n; i++)
buf[i] = toupper(buf[i]);
sendto(sfd, buf, n, 0, (struct sockaddr *)&cli_addr, len);
close(sfd);
return 0;
C#谁能给我个,C/S 通过Socket与线程通信的例子,,
S
Thread serThd=null;
Socket soc=null;
ServerStart()
soc=new ...
while(true)
////...
C
Thread thd=null;
Socket ...
//要求,只要C可以通过一个方法,或事件,完成与S的一次交互(即读写双边)即可.但是前提是用,双端都用Thread完成的,
我知道难,,,回答好,加30分,绝不食言
//======================客户端=========================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace S_c
public class Form1 : System.Windows.Forms.Form
private IPEndPoint ipendpoint;
private Socket localsocket;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private System.Windows.Forms.StatusBarPanel statusBarPanel2;
private System.ComponentModel.Container components = null;
public Form1()
InitializeComponent();
protected override void Dispose( bool disposing )
if( disposing )
if (components != null)
components.Dispose();
base.Dispose( disposing );
private void InitializeComponent()
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(208, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 56);
this.button1.TabIndex = 0;
this.button1.Text = "Begin Connect";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.label1.Location = new System.Drawing.Point(8, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 23);
this.label1.TabIndex = 1;
this.label1.Text = "HostIP:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.textBox1.Location = new System.Drawing.Point(64, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(128, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "127.0.0.1";
this.label2.Location = new System.Drawing.Point(8, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 23);
this.label2.TabIndex = 1;
this.label2.Text = "Port:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.textBox2.Location = new System.Drawing.Point(64, 64);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(128, 21);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "";
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Location = new System.Drawing.Point(24, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(296, 112);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Connect";
this.groupBox2.Controls.Add(this.richTextBox1);
this.groupBox2.Location = new System.Drawing.Point(24, 128);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(296, 176);
this.groupBox2.TabIndex = 4;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Receive Messages";
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox1.Location = new System.Drawing.Point(3, 17);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(290, 156);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
this.groupBox3.Controls.Add(this.richTextBox2);
this.groupBox3.Location = new System.Drawing.Point(24, 312);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(296, 176);
this.groupBox3.TabIndex = 5;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Send Messages";
this.richTextBox2.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox2.Location = new System.Drawing.Point(3, 17);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(290, 156);
this.richTextBox2.TabIndex = 0;
this.richTextBox2.Text = "";
this.button2.Location = new System.Drawing.Point(69, 496);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 6;
this.button2.Text = "SEND";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.button3.Location = new System.Drawing.Point(208, 496);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(64, 23);
this.button3.TabIndex = 6;
this.button3.Text = "REWRITE";
this.statusBar1.Location = new System.Drawing.Point(0, 535);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] this.statusBarPanel1,this.statusBarPanel2);
this.statusBar1.ShowPanels = true;
this.statusBar1.Size = new System.Drawing.Size(344, 22);
this.statusBar1.TabIndex = 7;
this.statusBarPanel1.Text = "Current Status:";
this.statusBarPanel2.Width = 350;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(344, 557);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.button2);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.button3);
this.Name = "Form1";
this.Text = "S_c";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
this.ResumeLayout(false);
[STAThread]
static void Main()
Application.Run(new Form1());
private void button1_Click(object sender, System.EventArgs e)
try
ipendpoint=new IPEndPoint(IPAddress.Parse(textBox1.Text.Trim()),int.Parse(textBox2.Text.Trim()));
catch
statusBar1.Panels[1].Text="Please input a valid value!";
return;
Thread thread=new Thread(new ThreadStart(accp));
thread.Start();
private void accp()
try
localsocket=new Socket(localsocket.Connect(ipendpoint);
catch
statusBar1.Panels[1].Text="Host not found!";
return;
statusBar1.Panels[1].Text="Has connected to host-->"+ipendpoint.ToString();
Thread thread=new Thread(new ThreadStart(getmsg));
thread.Start();
private void getmsg()
while(true)
byte[] buffer=new byte[128];
localsocket.Receive(buffer);
richTextBox1.AppendText("\n"+System.Text.Encoding.BigEndianUnicode.GetString(buffer));
private void button2_Click(object sender, System.EventArgs e)
buffer=System.Text.Encoding.BigEndianUnicode.GetBytes(richTextBox2.Text.Trim());
localsocket.Send(buffer);
//======================服务端=========================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace S_s
public class Form1 : System.Windows.Forms.Form
private IPEndPoint ipendpoint;
private Socket localsocket;
private Socket remotesocket;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private System.Windows.Forms.StatusBarPanel statusBarPanel2;
private System.ComponentModel.Container components = null;
public Form1()
InitializeComponent();
protected override void Dispose( bool disposing )
if( disposing )
if (components != null)
components.Dispose();
base.Dispose( disposing );
private void InitializeComponent()
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(208, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 56);
this.button1.TabIndex = 0;
this.button1.Text = "Begin Listen";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.label1.Location = new System.Drawing.Point(8, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 23);
this.label1.TabIndex = 1;
this.label1.Text = "HostIP:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.textBox1.Location = new System.Drawing.Point(64, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(128, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "127.0.0.1";
this.label2.Location = new System.Drawing.Point(8, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 23);
this.label2.TabIndex = 1;
this.label2.Text = "Port:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.textBox2.Location = new System.Drawing.Point(64, 64);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(128, 21);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "";
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Location = new System.Drawing.Point(24, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(296, 112);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Listen";
this.groupBox2.Controls.Add(this.richTextBox1);
this.groupBox2.Location = new System.Drawing.Point(24, 128);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(296, 176);
this.groupBox2.TabIndex = 4;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Receive Messages";
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox1.Location = new System.Drawing.Point(3, 17);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(290, 156);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
this.groupBox3.Controls.Add(this.richTextBox2);
this.groupBox3.Location = new System.Drawing.Point(24, 312);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(296, 176);
this.groupBox3.TabIndex = 5;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Send Messages";
this.richTextBox2.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox2.Location = new System.Drawing.Point(3, 17);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(290, 156);
this.richTextBox2.TabIndex = 0;
this.richTextBox2.Text = "";
this.button2.Location = new System.Drawing.Point(69, 496);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 6;
this.button2.Text = "SEND";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.button3.Location = new System.Drawing.Point(208, 496);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(64, 23);
this.button3.TabIndex = 6;
this.button3.Text = "REWRITE";
this.statusBar1.Location = new System.Drawing.Point(0, 535);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] this.statusBarPanel1,this.statusBarPanel2);
this.statusBar1.ShowPanels = true;
this.statusBar1.Size = new System.Drawing.Size(344, 22);
this.statusBar1.TabIndex = 7;
this.statusBarPanel1.Text = "Current Status:";
this.statusBarPanel2.Width = 350;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(344, 557);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.button2);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.button3);
this.Name = "Form1";
this.Text = "S_s";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
this.ResumeLayout(false);
[STAThread]
static void Main()
Application.Run(new Form1());
private void button1_Click(object sender, System.EventArgs e)
try
ipendpoint=new IPEndPoint(IPAddress.Parse(textBox1.Text.Trim()),int.Parse(textBox2.Text.Trim()));
catch
statusBar1.Panels[1].Text="Please input a valid value!";
return;
Thread thread=new Thread(new ThreadStart(accp));
thread.Start();
private void accp()
while(true)
try
localsocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
localsocket.Bind(ipendpoint);
localsocket.Listen(128);
catch
statusBar1.Panels[1].Text="Host not found or Listen has began!";
return;
statusBar1.Panels[1].Text="Listen has bengin!";
while(true)
remotesocket=localsocket.Accept();
Thread thread=new Thread(new ThreadStart(getmsg));
thread.Start();
private void getmsg()
while(true)
byte[] buffer=new byte[128];
remotesocket.Receive(buffer);
richTextBox1.AppendText("\n"+System.Text.Encoding.BigEndianUnicode.GetString(buffer));
private void button2_Click(object sender, System.EventArgs e)
byte[] buffer=System.Text.Encoding.BigEndianUnicode.GetBytes(DateTime.Now+"\n"+richTextBox2.Text.Trim());
remotesocket.Send(buffer);
参考技术A Socket异步通讯:
我做的一个网络联机小游戏上NetWork部分代码!可能能帮你解决问题!
Server:
http://code.google.com/p/shortboom/source/browse/trunk/NetWork/GameServer.cs
Client:
http://code.google.com/p/shortboom/source/browse/trunk/NetWork/GameClient.cs本回答被提问者采纳 参考技术B 可以使用 FlyTCPFramework
如果需要请我联系
也可以到我的网站来交流http://www.bg1jt.cn
以上是关于谁能给个C语言socket 通信,用UDP协议的代码例子。。的主要内容,如果未能解决你的问题,请参考以下文章