using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); private void button1_Click(object sender, EventArgs e) { // bouton connection _socket.Connect(new IpEndPoint(IpDestination,80)); } private void button2_Click(object sender, EventArgs e) {// bouton déconnection } private void button3_Click(object sender, EventArgs e) {// bouton envoyer } } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionusing System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); String IpDestination = "192.168.1.10"; private void button1_Click(object sender, EventArgs e) { // bouton connection IpEndPoint ipEP = new IpEndPoint(IpDestination,80); _socket.Connect(ipEP); } private void button2_Click(object sender, EventArgs e) {// bouton déconnection } private void button3_Click(object sender, EventArgs e) {//// bouton envoyer } } }
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); String IpDestination = "192.168.1.10", EdtBox; Byte[] MessageAEnvoyer; int OctetsEnvoyes; private void button1_Click(object sender, EventArgs e) { // bouton connection IPEndPoint ipEP = new IPEndPoint(IPAddress.Parse(IpDestination), 80); socket.Connect(ipEP); } private void button2_Click(object sender, EventArgs e) {// bouton déconnection socket.Close(); } private void button3_Click(object sender, EventArgs e) {//// bouton envoyer EdtBox = textBox1.Text; MessageAEnvoyer = System.Text.Encoding.ASCII.GetBytes(EdtBox); OctetsEnvoyes = socket.Send(MessageAEnvoyer, 0, MessageAEnvoyer.Length, SocketFlags.None); textBox1.Text=""; } }
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public Socket clientSocket; String IpSource = "192.168.1.46"; IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(IpSource), 80); clientSocket.receive(???????????????) //Je sais pas quoi mettre dedans
private void button4_Click(object sender, EventArgs e) { textBox2.Text = ""; socket.Receive(buffer2); System.Text.Encoding encoding = System.Text.Encoding.UTF8; textBox2.Text = encoding.GetString(buffer2); }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); endPoint = new IPEndPoint(IPAddress.Parse("192.168.1.77"), 80); // Ip et port de destination } Socket socket; // Nom de la fonction IPEndPoint endPoint; // adresse et port de destination byte[] buffer; // message qui sera envoyer int Bouton = 0; // connexion/déconnexion/Autorisation d'envoyer un message int size = 20; // Définie le nb de caractère max a envoyer Byte[] rep = new Byte[32767]; // message réponce reçu string stringData; // message réponce afficher string TextaEnvoyer; // Text transformer qui ira a la fonction envoyer int recv; // Nombre de caractère recu private void button1_Click(object sender, EventArgs e) { if (Bouton == 0)//Connexion { socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //Initialise la connexion udp socket.BeginConnect(endPoint, new AsyncCallback(BeginConnect), socket);//Initialise réception asynchrone des messages Bouton = 1; } else if (Bouton == 1)//Connexion { Bouton = 0;//Déconnexion socket.Shutdown(SocketShutdown.Both);//Ferme le protocole } } public void BeginConnect(IAsyncResult ar)//Reception asynchrone des messages { socket = (Socket)ar.AsyncState; socket.BeginReceive(rep, 0, size, SocketFlags.None, new AsyncCallback(CallbackMethod), socket); socket.EndConnect(ar); } private void CallbackMethod(IAsyncResult ar)//Reception asynchrone des messages { Socket remote = (Socket)ar.AsyncState; recv = remote.EndReceive(ar); socket.BeginReceive(rep, 0, size, SocketFlags.None, new AsyncCallback(CallbackMethod), socket); this.Invoke(new EventHandler(DisplayText)); } private void DisplayText(object sender, EventArgs e)//Affiche les messages { stringData = Encoding.ASCII.GetString(rep, 0, recv); richTextBox1.Text += stringData + System.Environment.NewLine; } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { socket.Close(); } private void button2_Click(object sender, EventArgs e) { TextaEnvoyer = textBox1.Text; buffer = Encoding.ASCII.GetBytes("<"+TextaEnvoyer+">"); socket.SendTo(buffer, endPoint); //buffer= message en byte, endPoint est l'adresse de destination } } }